# Replication code for Foreign Direct Investment and Repression: An~Analysis Across Industry Sectors

# Note: If search for each table's header in the paper and online appendix and you will get to the right line of code for that particular result. However, you should run all code in the order given, and run all code until you get to the table you wish to replicate. Do not run separate sections of code only. I sometimes create variables 'on the go' which are being reused later; I also save some output objects under the same name and overwrite the object with a different one later. This code has been replicated by a research assistant table by table, and in this order. All errors remain my own. 

# set working directory (change accordingly!)
# setwd("~/Desktop/FDI & Pol/jhr/Round4/Replication Upload")

# You may have to install these libraries first. For exact replication, it may be necessary to use the exact same versions as this Rcsript

# install.packages("plm")
# install.packages("rms")
# install.packages("lmtest")
# install.packages("stargazer")
# install.packages("pwr")

library(plm)
library(rms) 
library(lmtest)
library(stargazer)
library(pwr)



sessionInfo() # the results were created (and replicated by research assistants) using these versions, which you may have to install to get the exact same results

# R version 3.1.3 (2015-03-09)
# Platform: x86_64-apple-darwin13.4.0 (64-bit)
# Running under: OS X 10.10.1 (Yosemite)
# 
# locale:
#   [1] en_GB.UTF-8/en_GB.UTF-8/en_GB.UTF-8/C/en_GB.UTF-8/en_GB.UTF-8
# 
# attached base packages:
#   [1] grid      stats     graphics  grDevices utils     datasets  methods   base     
# 
# other attached packages:
#   [1] rms_4.3-0         SparseM_1.6       gridExtra_0.9.1   pscl_1.4.9        devtools_1.6.1    forecast_5.9      timeDate_3012.100 stargazer_5.1    
# [9] plyr_1.8.1        tseries_0.10-34   pcse_1.9          arm_1.8-4         lme4_1.1-7        Rcpp_0.12.0       Matrix_1.2-2      gplots_2.15.0    
# [17] Hmisc_3.15-0      ggplot2_1.0.1     survival_2.38-1   lattice_0.20-31   lmtest_0.9-32     plm_1.3-1         zoo_1.7-11        sandwich_2.3-2   
# [25] MASS_7.3-44       Formula_1.1-2     nlme_3.1-111      bdsmatrix_1.3-2  
# 
# loaded via a namespace (and not attached):
#   [1] abind_1.4-3         acepack_1.3-3.3     bitops_1.0-6        caTools_1.17.1      cluster_2.0.1       coda_0.17-1         codetools_0.2-10   
# [8] colorspace_1.2-4    digest_0.6.4        foreign_0.8-63      fracdiff_1.4-2      gdata_2.17.0        gtable_0.1.2        gtools_3.4.1       
# [15] KernSmooth_2.23-14  latticeExtra_0.6-26 minqa_1.2.4         multcomp_1.4-0      munsell_0.4.2       mvtnorm_1.0-2       nloptr_1.0.4       
# [22] nnet_7.3-9          parallel_3.1.3      polspline_1.1.9     proto_0.3-10        quadprog_1.5-5      quantreg_5.11       RColorBrewer_1.1-2 
# [29] reshape2_1.4.1      rpart_4.1-9         scales_0.2.4        splines_3.1.3       stringr_0.6.2       TH.data_1.0-6       tools_3.1.3   


# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 
# loading data and functions to use; pre-run before running any models
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 

# FUNCTION to Lag panel data by Chris Adolph

# Usage:
#
# laggeddata <- lagpanel(x,  # An n x k matrix of data to be lagged;
# must be in time series order and stacked by
# unit
#                        c,  # An n x 1 vector of group numbers
#                        t,  # An n x 1 vector of periods
#                        lagnum, # The desired lag to report

lagpanel <- function(x,c,t,lagnum) {
  outclass <- "matrix"
  if (any(class(x)=="data.frame")) outclass <- "data.frame"
  x <- as.matrix(x)
  outnames <- colnames(x)
  c <- as.matrix(c)
  t <- as.matrix(t)
  listc <- unique(c)
  outmat <- matrix(NA,nrow=nrow(x),ncol=ncol(x))
  runningtotal <- 0
  for (i in 1:nrow(listc)) {
    numtper <- length(unique(t[c==listc[i,]]))
    xc <- as.matrix(x[c==listc[i,],])
    if (nrow(xc)>numtper)
      stop(paste("Duplicate time periods in data for unit", listc[i]))
    if (numtper>lagnum) {
      outmat[(runningtotal+1+lagnum):(runningtotal + numtper),] <-
        xc[1:(nrow(xc)-lagnum),]
    }
    runningtotal <- runningtotal + numtper
  }
  if (outclass=="data.frame") outmat <- as.data.frame(outmat)
  colnames(outmat) <- outnames
  outmat
}

my.lag.vars <- function(x,vars,prefix="Lag_"){
  
  if (!all(vars%in%colnames(x))) stop("some vars are not in colnames(x)")
  for (i in vars){
    newname <- paste(prefix,i,sep="")
    x[newname] <- lagpanel(x[i],x$country,x$year,1) #we don't need cbind since we already create new variables with new name
  } #end for
  
  return(x)
} ##end function 

## FUNCTION to run my.lag.vars on all matrices in a list 
##
my.lag.vars.list <- function(L,cols){ ## L is a list of matrices, cols the columns to be lagged in each one
  L.lag <- list()
  for (i in 1:length(L)){
    L.lag[[i]] <- my.lag.vars(L[[i]],cols)
    names(L.lag)[i] <- names(L)[i]
  } ## end for i
  return(L.lag)
} ## end function
# # # # # # # #       END OF FUNCTIONS TO RUN       # # # # # # # # # # # # # # # # 












# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 
# load human rights data set and select only non-OECD countries;
# call the subset "t.nono"
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 

load("replicationdata.Rdata") # this data file needs to be in the folder of your working directory
t.nono <- replicationdata[replicationdata$oecd==0, ] # select developing nations only (non-OECD)
t.nono <- droplevels(t.nono) # drop the unused levels (http://stackoverflow.com/questions/17217951/how-can-i-drop-unused-levels-from-a-data-frame)








# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 
# Main models for paper
#
# I create data sets with variables for each particular model 
# to preserve the largest possible sample size each time
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 


# create data ##########

# for total U.S. FDI:
data.CIRI.PHY.LDV <- na.omit(t.nono[,cbind("CIRI_PHYSINT","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_totalpGDP","Lag_CIRI_PHYSINT", "country","year")])
data.PTS.LDV <- na.omit(t.nono[,cbind("PTS_ai_reversed","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_totalpGDP", "Lag_PTS_ai_reversed", "country","year")])
data.Fariss.LDV <- na.omit(t.nono[,cbind("Fariss_HR","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_totalpGDP", "Lag_Fariss_HR","country","year")]) 
data.Fariss.LDV$country <- factor(as.character(data.Fariss.LDV$country)) #from 192 to 118
# For total UN FDI:
data.CIRI.PHY1.LDV <- na.omit(t.nono[,cbind("CIRI_PHYSINT","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_log_UN_FDI_stock_pgdp" ,"Lag_CIRI_PHYSINT","country","year")])
data.PTS1.LDV <- na.omit(t.nono[,cbind("PTS_ai_reversed","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_log_UN_FDI_stock_pgdp","Lag_PTS_ai_reversed", "country","year")])
data.Fariss1.LDV <- na.omit(t.nono[,cbind("Fariss_HR","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_log_UN_FDI_stock_pgdp", "Lag_Fariss_HR","country","year")])


# Creating table "Total FDI and Personal Integrity Rights Protection (1983-2010)" in the main part of the paper ####
# US FDI ologit with robust standard errors clustered by country
CIRI.logit.LDV <- lrm(CIRI_PHYSINT ~ Lag_lognonnegUS_fdi_totalpGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_CIRI_PHYSINT,data=data.CIRI.PHY.LDV,x=TRUE, y=TRUE)
CIRI.logit.LDV.corr <- robcov(CIRI.logit.LDV,data.CIRI.PHY.LDV$country) #robust errors clustered by country 

PTS.logit.LDV <- lrm(PTS_ai_reversed ~ Lag_lognonnegUS_fdi_totalpGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_PTS_ai_reversed,data=data.PTS.LDV,x=TRUE, y=TRUE)
PTS.logit.LDV.corr <- robcov(PTS.logit.LDV,data.PTS.LDV$country) 

#Fariss pooled OLS with PCSE
data.Fariss.LDV <- as.data.frame(data.Fariss.LDV)
form   <- Fariss_HR ~ Lag_lognonnegUS_fdi_totalpGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_Fariss_HR
res.po <- plm(form,model="pooling",index=c("country","year"),data=data.Fariss.LDV)
summary(res.po)      
## PCSE a la Beck-Katz
res.BKtime  <- vcovBK(x=res.po,cluster="time")
ct.time     <- coeftest(res.po,vcov=res.BKtime)
print(ct.time)

# rename lagged dependent variable (new name: "Past") to get them into one row with stargazer; replace plm se's with corrected ones
names(CIRI.logit.LDV.corr$coefficients)
names(CIRI.logit.LDV.corr$coefficients)[names(CIRI.logit.LDV.corr$coefficients) == "Lag_CIRI_PHYSINT"] <- "Past"
rownames(CIRI.logit.LDV.corr$var)[rownames(CIRI.logit.LDV.corr$var)== "Lag_CIRI_PHYSINT"] <- "Past"
colnames(CIRI.logit.LDV.corr$var)[colnames(CIRI.logit.LDV.corr$var)== "Lag_CIRI_PHYSINT"] <- "Past"

names(PTS.logit.LDV.corr$coefficients)
names(PTS.logit.LDV.corr$coefficients)[names(PTS.logit.LDV.corr$coefficients) == "Lag_PTS_ai_reversed"] <- "Past"
rownames(PTS.logit.LDV.corr$var)[rownames(PTS.logit.LDV.corr$var)== "Lag_PTS_ai_reversed"] <- "Past"
colnames(PTS.logit.LDV.corr$var)[colnames(PTS.logit.LDV.corr$var)== "Lag_PTS_ai_reversed"] <- "Past"

res.po$vcov <- res.BKtime  # replacing the first uncorrected residual covariance structure with the corrected one see str()
names(res.po$coefficients)[names(res.po$coefficients) == "Lag_Fariss_HR"] <- "Past"
rownames(res.po$vcov)[rownames(res.po$vcov)== "Lag_Fariss_HR"] <- "Past"
colnames(res.po$vcov)[colnames(res.po$vcov)== "Lag_Fariss_HR"] <- "Past"

# Results Total UN FDI 
CIRI.logit.LDV.UN <- lrm(CIRI_PHYSINT ~ Lag_log_UN_FDI_stock_pgdp + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_CIRI_PHYSINT,data=data.CIRI.PHY1.LDV ,x=TRUE, y=TRUE)
CIRI.logit.LDV.UN.corr <- robcov(CIRI.logit.LDV.UN,data.CIRI.PHY1.LDV$country) #robust errors clustered by country 

PTS.logit.LDV.UN <- lrm(PTS_ai_reversed ~ Lag_log_UN_FDI_stock_pgdp + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_PTS_ai_reversed,data=data.PTS1.LDV,x=TRUE, y=TRUE)
PTS.logit.LDV.UN.corr <- robcov(PTS.logit.LDV.UN,data.PTS1.LDV$country) 

data.Fariss.1.LDV <- as.data.frame(data.Fariss1.LDV)
form   <- Fariss_HR ~ Lag_log_UN_FDI_stock_pgdp + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_Fariss_HR
res.po1 <- plm(form,model="pooling",index=c("country","year"),data=data.Fariss1.LDV)
summary(res.po1)      
## PCSE a la Beck-Katz
res.BKtime1  <- vcovBK(x=res.po1,cluster="time")
ct.time1     <- coeftest(res.po1,vcov=res.BKtime1)
print(ct.time1) 

# rename lagged values to "Past" so they go into one row in overall table
names(CIRI.logit.LDV.UN.corr$coefficients)
names(CIRI.logit.LDV.UN.corr$coefficients)[names(CIRI.logit.LDV.UN.corr$coefficients) == "Lag_CIRI_PHYSINT"] <- "Past"
rownames(CIRI.logit.LDV.UN.corr$var)[rownames(CIRI.logit.LDV.UN.corr$var)== "Lag_CIRI_PHYSINT"] <- "Past"
colnames(CIRI.logit.LDV.UN.corr$var)[colnames(CIRI.logit.LDV.UN.corr$var)== "Lag_CIRI_PHYSINT"] <- "Past"
names(PTS.logit.LDV.UN.corr$coefficients)
names(PTS.logit.LDV.UN.corr$coefficients)[names(PTS.logit.LDV.UN.corr$coefficients) == "Lag_PTS_ai_reversed"] <- "Past"
rownames(PTS.logit.LDV.UN.corr$var)[rownames(PTS.logit.LDV.UN.corr$var)== "Lag_PTS_ai_reversed"] <- "Past"
colnames(PTS.logit.LDV.UN.corr$var)[colnames(PTS.logit.LDV.UN.corr$var)== "Lag_PTS_ai_reversed"] <- "Past"

res.po1$vcov <- res.BKtime1  # replacing the first uncorrected residual covariance structure with the corrected one see str()
names(res.po1$coefficients)[names(res.po1$coefficients) == "Lag_Fariss_HR"] <- "Past"
rownames(res.po1$vcov)[rownames(res.po1$vcov)== "Lag_Fariss_HR"] <- "Past"
colnames(res.po1$vcov)[colnames(res.po1$vcov)== "Lag_Fariss_HR"] <- "Past"

# Table: "Total FDI and Personal Integrity Rights Protection (1983-2010)" ####
varnames <- c("Global FDI p. GDP","US FDI p. GDP", "Trade", "GDP p. capita", "Population", "Democracy", "Conflict","Past")
dvnames <- c("CIRI Integrity","PTS Amnesty","Latent Scores","CIRI Integrity","PTS Amnesty","Latent Scores")
stargazer(CIRI.logit.LDV.UN.corr, PTS.logit.LDV.UN.corr,res.po1,CIRI.logit.LDV.corr,PTS.logit.LDV.corr,res.po,
          dep.var.labels=dvnames,
          covariate.labels=varnames,
          omit=c("y>=1","y>=2","y>=3","y>=4","y>=5","y>=6","y>=7","y>=8","factor","Constant","(Intercept)"),
          type="text",omit.stat=c("chi2","f","ser"),notes=c("Ordered logit with country-clustered standard errors in parentheses below the coefficient estimates.","OLS with panel corrected standard errors (PCSE) in parentheses below the coefficient estimates.","FDIpGDP, GDPpc, population, trade are logged. Past is the lagged dependent variable (LDV)."),font.size="scriptsize",notes.align="l",title="Total FDI and Personal Integrity Rights Protection (1983-2010)",label="total.tex",out="total.tex")


# for later: save the R output objects as baseline model
CIRI.logit.LDV.corr.baseline <- CIRI.logit.LDV.corr
PTS.logit.LDV.corr.baseine <- PTS.logit.LDV.corr
res.po.baseline <- res.po


# # Sectoral (exclusive resource) FDI Models BASELINE ####
# data for SECTORAL MODELS  excl size controls###########
data.CIRI.PHY.excl.LDV <- na.omit(t.nono[,cbind("CIRI_PHYSINT","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_foodpGDP","Lag_lognonnegUS_fdi_chemicalpGDP","Lag_lognonnegUS_fdi_prim_fab_metalpGDP","Lag_lognonnegUS_fdi_machinerypGDP","Lag_lognonnegUS_fdi_electricalpGDP","Lag_lognonnegUS_fdi_transportpGDP","Lag_lognonnegUS_fdi_whole_tradepGDP","Lag_lognonnegUS_fdi_depositorypGDP","Lag_CIRI_PHYSINT", "country","year")])
data.PTS.excl.LDV  <- na.omit(t.nono[,cbind("PTS_ai_reversed","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_foodpGDP","Lag_lognonnegUS_fdi_chemicalpGDP","Lag_lognonnegUS_fdi_prim_fab_metalpGDP","Lag_lognonnegUS_fdi_machinerypGDP","Lag_lognonnegUS_fdi_electricalpGDP","Lag_lognonnegUS_fdi_transportpGDP","Lag_lognonnegUS_fdi_whole_tradepGDP","Lag_lognonnegUS_fdi_depositorypGDP","Lag_PTS_ai_reversed","country","year")])
data.Fariss.excl.LDV <- na.omit(t.nono[,cbind("Fariss_HR","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_foodpGDP","Lag_lognonnegUS_fdi_chemicalpGDP","Lag_lognonnegUS_fdi_prim_fab_metalpGDP","Lag_lognonnegUS_fdi_machinerypGDP","Lag_lognonnegUS_fdi_electricalpGDP","Lag_lognonnegUS_fdi_transportpGDP","Lag_lognonnegUS_fdi_whole_tradepGDP","Lag_lognonnegUS_fdi_depositorypGDP","Lag_Fariss_HR","country","year")])


CIRI.logit.LDV.excl <- lrm(CIRI_PHYSINT ~ Lag_lognonnegUS_fdi_foodpGDP + Lag_lognonnegUS_fdi_chemicalpGDP + Lag_lognonnegUS_fdi_prim_fab_metalpGDP + Lag_lognonnegUS_fdi_machinerypGDP + Lag_lognonnegUS_fdi_electricalpGDP + Lag_lognonnegUS_fdi_transportpGDP + Lag_lognonnegUS_fdi_whole_tradepGDP + Lag_lognonnegUS_fdi_depositorypGDP+ Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_CIRI_PHYSINT,data=data.CIRI.PHY.excl.LDV  ,x=TRUE, y=TRUE)
CIRI.logit.LDV.excl.corr <- robcov(CIRI.logit.LDV.excl,data.CIRI.PHY.excl.LDV$country) #robust errors 

PTS.logit.LDV.excl <- lrm(PTS_ai_reversed ~ Lag_lognonnegUS_fdi_foodpGDP + Lag_lognonnegUS_fdi_chemicalpGDP + Lag_lognonnegUS_fdi_prim_fab_metalpGDP + Lag_lognonnegUS_fdi_machinerypGDP + Lag_lognonnegUS_fdi_electricalpGDP + Lag_lognonnegUS_fdi_transportpGDP + Lag_lognonnegUS_fdi_whole_tradepGDP + Lag_lognonnegUS_fdi_depositorypGDP+ Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_PTS_ai_reversed,data=data.PTS.excl.LDV,x=TRUE, y=TRUE)
PTS.logit.LDV.excl.corr <- robcov(PTS.logit.LDV.excl,data.PTS.excl.LDV$country) 

data.Fariss.excl.LDV <- as.data.frame(data.Fariss.excl.LDV)
form   <- Fariss_HR ~  Lag_lognonnegUS_fdi_foodpGDP + Lag_lognonnegUS_fdi_chemicalpGDP + Lag_lognonnegUS_fdi_prim_fab_metalpGDP + Lag_lognonnegUS_fdi_machinerypGDP + Lag_lognonnegUS_fdi_electricalpGDP + Lag_lognonnegUS_fdi_transportpGDP + Lag_lognonnegUS_fdi_whole_tradepGDP + Lag_lognonnegUS_fdi_depositorypGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_Fariss_HR
res.po.excl <- plm(form,model="pooling",index=c("country","year"),data=data.Fariss.excl.LDV)
summary(res.po.excl)      
## PCSE a la Beck-Katz
res.BKtime.excl  <- vcovBK(x=res.po.excl,cluster="time")
ct.time.excl     <- coeftest(res.po.excl,vcov=res.BKtime.excl)
print(ct.time.excl)

# rename lagged values to get them into one row
names(CIRI.logit.LDV.excl.corr$coefficients)
names(CIRI.logit.LDV.excl.corr$coefficients)[names(CIRI.logit.LDV.excl.corr$coefficients) == "Lag_CIRI_PHYSINT"] <- "Past"
rownames(CIRI.logit.LDV.excl.corr$var)[rownames(CIRI.logit.LDV.excl.corr$var)== "Lag_CIRI_PHYSINT"] <- "Past"
colnames(CIRI.logit.LDV.excl.corr$var)[colnames(CIRI.logit.LDV.excl.corr$var)== "Lag_CIRI_PHYSINT"] <- "Past"
names(PTS.logit.LDV.excl.corr$coefficients)
names(PTS.logit.LDV.excl.corr$coefficients)[names(PTS.logit.LDV.excl.corr$coefficients) == "Lag_PTS_ai_reversed"] <- "Past"
rownames(PTS.logit.LDV.excl.corr$var)[rownames(PTS.logit.LDV.excl.corr$var)== "Lag_PTS_ai_reversed"] <- "Past"
colnames(PTS.logit.LDV.excl.corr$var)[colnames(PTS.logit.LDV.excl.corr$var)== "Lag_PTS_ai_reversed"] <- "Past"


res.po.excl$vcov <- res.BKtime.excl  # replacing the first uncorrected residual covariance structure with the corrected one see str()
names(res.po.excl$coefficients)[names(res.po.excl$coefficients) == "Lag_Fariss_HR"] <- "Past"
rownames(res.po.excl$vcov)[rownames(res.po.excl$vcov)== "Lag_Fariss_HR"] <- "Past"
colnames(res.po.excl$vcov)[colnames(res.po.excl$vcov)== "Lag_Fariss_HR"] <- "Past"

# save res for larger table:
CIRI.logit.LDV.excl.corr.baseline <- CIRI.logit.LDV.excl.corr
PTS.logit.LDV.excl.corr.baseline <- PTS.logit.LDV.excl.corr
res.po.excl.baseline <- res.po.excl


# Creating table "Sectoral FDI and Personal Integrity Rights Protection (1983-2010)" in the main part of the paper ####

# data for SECTORAL MODELS incl sector size controls###########
data.CIRI.PHY.excl.LDV <- na.omit(t.nono[,cbind("CIRI_PHYSINT","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_foodpGDP","Lag_lognonnegUS_fdi_chemicalpGDP","Lag_lognonnegUS_fdi_prim_fab_metalpGDP","Lag_lognonnegUS_fdi_machinerypGDP","Lag_lognonnegUS_fdi_electricalpGDP","Lag_lognonnegUS_fdi_transportpGDP","Lag_lognonnegUS_fdi_whole_tradepGDP","Lag_lognonnegUS_fdi_depositorypGDP",
                                                "Lag_CIRI_PHYSINT","Lag_log_Employment_Services", "Lag_log_Employment_Industry"  , "country","year")])
data.PTS.excl.LDV  <- na.omit(t.nono[,cbind("PTS_ai_reversed","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_foodpGDP","Lag_lognonnegUS_fdi_chemicalpGDP","Lag_lognonnegUS_fdi_prim_fab_metalpGDP","Lag_lognonnegUS_fdi_machinerypGDP","Lag_lognonnegUS_fdi_electricalpGDP","Lag_lognonnegUS_fdi_transportpGDP","Lag_lognonnegUS_fdi_whole_tradepGDP","Lag_lognonnegUS_fdi_depositorypGDP",
                                            "Lag_PTS_ai_reversed", "Lag_log_Employment_Services", "Lag_log_Employment_Industry"  ,"country","year")])
data.Fariss.excl.LDV <- na.omit(t.nono[,cbind("Fariss_HR","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_foodpGDP","Lag_lognonnegUS_fdi_chemicalpGDP","Lag_lognonnegUS_fdi_prim_fab_metalpGDP","Lag_lognonnegUS_fdi_machinerypGDP","Lag_lognonnegUS_fdi_electricalpGDP","Lag_lognonnegUS_fdi_transportpGDP","Lag_lognonnegUS_fdi_whole_tradepGDP","Lag_lognonnegUS_fdi_depositorypGDP",
                                              "Lag_Fariss_HR","Lag_log_Employment_Services", "Lag_log_Employment_Industry"  ,"country","year")])


CIRI.logit.LDV.excl <- lrm(CIRI_PHYSINT ~ Lag_lognonnegUS_fdi_foodpGDP + Lag_lognonnegUS_fdi_chemicalpGDP + Lag_lognonnegUS_fdi_prim_fab_metalpGDP + Lag_lognonnegUS_fdi_machinerypGDP + Lag_lognonnegUS_fdi_electricalpGDP + Lag_lognonnegUS_fdi_transportpGDP + Lag_lognonnegUS_fdi_whole_tradepGDP + Lag_lognonnegUS_fdi_depositorypGDP+ Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + 
                             Lag_log_Employment_Services + Lag_log_Employment_Industry + 
                             + Lag_CIRI_PHYSINT,data=data.CIRI.PHY.excl.LDV  ,x=TRUE, y=TRUE)
CIRI.logit.LDV.excl.corr <- robcov(CIRI.logit.LDV.excl,data.CIRI.PHY.excl.LDV$country) #robust errors 

PTS.logit.LDV.excl <- lrm(PTS_ai_reversed ~ Lag_lognonnegUS_fdi_foodpGDP + Lag_lognonnegUS_fdi_chemicalpGDP + Lag_lognonnegUS_fdi_prim_fab_metalpGDP + Lag_lognonnegUS_fdi_machinerypGDP + Lag_lognonnegUS_fdi_electricalpGDP + Lag_lognonnegUS_fdi_transportpGDP + Lag_lognonnegUS_fdi_whole_tradepGDP + Lag_lognonnegUS_fdi_depositorypGDP           + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + 
                            Lag_log_Employment_Services + Lag_log_Employment_Industry + 
                            + Lag_PTS_ai_reversed,data=data.PTS.excl.LDV,x=TRUE, y=TRUE)
PTS.logit.LDV.excl.corr <- robcov(PTS.logit.LDV.excl,data.PTS.excl.LDV$country) 

data.Fariss.excl.LDV <- as.data.frame(data.Fariss.excl.LDV)
form   <- Fariss_HR ~  Lag_lognonnegUS_fdi_foodpGDP + Lag_lognonnegUS_fdi_chemicalpGDP + Lag_lognonnegUS_fdi_prim_fab_metalpGDP + Lag_lognonnegUS_fdi_machinerypGDP + Lag_lognonnegUS_fdi_electricalpGDP + Lag_lognonnegUS_fdi_transportpGDP + Lag_lognonnegUS_fdi_whole_tradepGDP + Lag_lognonnegUS_fdi_depositorypGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl  + 
  Lag_log_Employment_Services + Lag_log_Employment_Industry + 
  + Lag_Fariss_HR
res.po.excl <- plm(form,model="pooling",index=c("country","year"),data=data.Fariss.excl.LDV)
summary(res.po.excl)      
## PCSE a la Beck-Katz
res.BKtime.excl  <- vcovBK(x=res.po.excl,cluster="time")
ct.time.excl     <- coeftest(res.po.excl,vcov=res.BKtime.excl)
print(ct.time.excl)

# rename lagged values to get them into one row
names(CIRI.logit.LDV.excl.corr$coefficients)
names(CIRI.logit.LDV.excl.corr$coefficients)[names(CIRI.logit.LDV.excl.corr$coefficients) == "Lag_CIRI_PHYSINT"] <- "Past"
rownames(CIRI.logit.LDV.excl.corr$var)[rownames(CIRI.logit.LDV.excl.corr$var)== "Lag_CIRI_PHYSINT"] <- "Past"
colnames(CIRI.logit.LDV.excl.corr$var)[colnames(CIRI.logit.LDV.excl.corr$var)== "Lag_CIRI_PHYSINT"] <- "Past"
names(PTS.logit.LDV.excl.corr$coefficients)
names(PTS.logit.LDV.excl.corr$coefficients)[names(PTS.logit.LDV.excl.corr$coefficients) == "Lag_PTS_ai_reversed"] <- "Past"
rownames(PTS.logit.LDV.excl.corr$var)[rownames(PTS.logit.LDV.excl.corr$var)== "Lag_PTS_ai_reversed"] <- "Past"
colnames(PTS.logit.LDV.excl.corr$var)[colnames(PTS.logit.LDV.excl.corr$var)== "Lag_PTS_ai_reversed"] <- "Past"

res.po.excl$vcov <- res.BKtime.excl  # replacing the first uncorrected residual covariance structure with the corrected one see str()
names(res.po.excl$coefficients)[names(res.po.excl$coefficients) == "Lag_Fariss_HR"] <- "Past"
rownames(res.po.excl$vcov)[rownames(res.po.excl$vcov)== "Lag_Fariss_HR"] <- "Past"
colnames(res.po.excl$vcov)[colnames(res.po.excl$vcov)== "Lag_Fariss_HR"] <- "Past"

# save models
CIRI.logit.LDV.excl.corr.secsize <- CIRI.logit.LDV.excl.corr
PTS.logit.LDV.excl.corr.secsize<- PTS.logit.LDV.excl.corr
res.po.excl.secsize <- res.po.excl


#?large table with all sectoral models
varnames <- c("Food FDI","Chemical FDI","Metal FDI","Machinery FDI","Electrical FDI","Transport FDI", "Wholesale FDI","Depository FDI", "Trade", "GDP p. capita", "Population", "Democracy", "Conflict","Employm. Services","Employm. Industry", "Past")
dvnames <- c("CIRI Integrity","PTS Amnesty","Latent Scores","CIRI Integrity","PTS Amnesty","Latent Scores")

stargazer(CIRI.logit.LDV.excl.corr.baseline,PTS.logit.LDV.excl.corr.baseline,res.po.excl.baseline, 
          CIRI.logit.LDV.excl.corr.secsize,PTS.logit.LDV.excl.corr.secsize,res.po.excl.secsize, 
          covariate.labels=varnames, dep.var.labels=dvnames, 
          column.labels   = c("Baseline", "Sector Size Included"),
          column.separate = c(3, 3),
          omit=c("y>=1","y>=2","y>=3","y>=4","y>=5","y>=6","y>=7","y>=8","factor","Constant","(Intercept)"), 
          omit.stat = c("f","ser","chi2"),
          nobs=T,title="Sectoral FDI and Personal Integrity Rights Protection (1983-2010)",no.space=T,font.size="scriptsize", notes=c("Ordered logit with country-clustered standard errors. OLS with panel corrected standard errors.","All FDI is divided by GDP, then logged. GDP p. capita, population and trade are logged.","Past is the lagged dependent variable (LDV)."),notes.align="l",type="text",label="Sectoral.base.size.tex",out="Sectoral.base.size.tex")
























# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 

#### ______ROBUSTNESS SECTION______ ####

# These tables appear in the online appendix

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 

load("replicationdata.Rdata") # re-load just in case
t.nono <- replicationdata[replicationdata$oecd==0, ] # select developing nations only (non-OECD)
t.nono <- droplevels(t.nono) # drop the unused levels (http://stackoverflow.com/questions/17217951/how-can-i-drop-unused-levels-from-a-data-frame)




##############################################################################################

# Creating table "List of Countries"

##############################################################################################

# re-use object from earlier, data set with largest sample and no of countries: data.Fariss1.LDV

countrylist <- unique(data.Fariss1.LDV$country) #121
# countrylist  <- unique(t.nono$country) #158
# countrylist <- unique(data.CIRI.PHY.LDV$country) #118
tabcountry <- matrix(ncol=4,nrow=31) 
tabcountry[,1] <- as.character(countrylist[1:31])
tabcountry[,2] <- as.character(countrylist[32:62])
tabcountry[,3] <- as.character(countrylist[63:93])
rest <- as.character(countrylist[94:121])
lastcolumn <- c(rest,"","","") 
tabcountry[,4] <- lastcolumn

# only re-run if data change, I manyally changed the .tex
# stargazer(tabcountry,type="text",title="List of Countries",no.space=T,font.size="footnotesize",label="tabcountry.tex",out="tabcountry.tex")












###########################################
#Creating "Correlation matrix (I) to (III)
###########################################
#stargazer did not give stars, but nearly all are significant! #when doing stargazer(correlation.matrix[,1:10],type="latex") I can split up the table; but it displays all correlations, not diagonally empty fields for those already in rows (columns)
corstars <- function(x){
  x <- as.matrix(x)
  R <- rcorr(x, type="spearman")$r   
  p <- rcorr(x)$P
  # n <- rcorr(x)$n
  mystars <- ifelse(p < .01, "***", ifelse(p < .05, "** ", ifelse(p < .1, "*  ", "   ")))
  R <- format(round(R, 3))
  Rnew <- matrix(paste(R, mystars, sep=""), ncol=ncol(x)) #removed n here
  Rnew[upper.tri(Rnew,diag=TRUE)] <- ""
  Rnew <- data.frame(Rnew)[-1,-length(colnames(x))]
  rownames(Rnew) <- colnames(x)[-1]
  colnames(Rnew) <- colnames(x)[-length(colnames(x))]
  return(Rnew)
}

# Creating "Correlation matrix (I) to (III): ###
S <- corstars(data.sum[,-c(1,2)])
dim(S) #19 19

# # only re-run if data change, I manually changed the .tex
# stargazer(S[,1:7],type="text",title="Correlation Matrix (I)",no.space=T,font.size="scriptsize",notes.align="l",label="Biv.tex",out="Biv1.tex",float.env="sidewaystable")
# stargazer(S[,8:14],type="text",title="Correlation Matrix (II)",no.space=T,font.size="scriptsize",notes.align="l",label="Biv2.tex",out="Biv2.tex",float.env="sidewaystable")
# stargazer(S[,15:19],type="text",title="Correlation Matrix (III)",no.space=T,font.size="scriptsize",notes.align="l",label="Biv3.tex",out="Biv3.tex",float.env="sidewaystable")













# # # #  # # # #  # # # #  # # # #  # # # #  # # # #  # # # #  # # # #  # # # #  # # # #  # # # #  # # # #  # # # #  
# Creating table "Descriptive Variable Information" ####
# # # #  # # # #  # # # #  # # # #  # # # #  # # # #  # # # #  # # # #  # # # #  # # # #  # # # #  # # # #  # # # #  

# Preparation: select variables to use for summary statistics 

data.sum <- t.nono[,cbind("country","year",
                          "CIRI_PHYSINT",
                          "PTS_ai_reversed",
                          "Fariss_HR",        
                          
                          "Lag_log_UN_FDI_stock_pgdp", 
                          "Lag_lognonnegUS_fdi_totalpGDP",
                          
                          "Lag_lognonnegUS_fdi_petrolpGDP",
                          "Lag_lognonnegUS_fdi_miningpGDP",  
                          "Lag_lognonnegUS_fdi_foodpGDP",
                          "Lag_lognonnegUS_fdi_chemicalpGDP",
                          "Lag_lognonnegUS_fdi_prim_fab_metalpGDP",
                          "Lag_lognonnegUS_fdi_machinerypGDP",
                          "Lag_lognonnegUS_fdi_electricalpGDP",
                          "Lag_lognonnegUS_fdi_transportpGDP",
                          "Lag_lognonnegUS_fdi_whole_tradepGDP",
                          "Lag_lognonnegUS_fdi_depositorypGDP",
                          
                          "Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl"
)]
dim(data.sum) #4582   22
data.sum$country <- factor(data.sum$country) #applying factor() to reduce levels of country variable
colnames(data.sum) <- c("country","year",
                        
                        "CIRI Phys. Integrity",
                        "PTS Amnesty",
                        "Latent Scores (Fariss)",
                        
                        "Global FDI/GDP",
                        "U.S. Total FDI/GDP",
                        
                        "Petrol FDI/GDP",
                        "Mining FDI/GDP",
                        "Food FDI/GDP",
                        "Chemical FDI/GDP",
                        "Metal FDI/GDP",
                        "Machinery FDI/GDP",
                        "Electrical FDI/GDP",
                        "Transport FDI/GDP",
                        "Whole. Trade FDI/GDP",
                        "Deposit. Inst. FDI/GDP",
                        
                        "Trade","GDP p. capita","Population","Democracy","Conflict"                       
)
# end of: Preparation: create subset of data to use for summary statistics

# Table: Descriptive Variable Information ####
library(stargazer)
stargazer(data.sum[,-c(1,2)],title="Descriptive Variable Information",  out="SummaryStats.tex",
          font.size="footnotesize",label="summarystats",notes="All investment variables, as well as Trade, GDPpc and Population are logged.",no.space=T,type="text") #without country, year









###########################################
# Figure: Time trends for three measures of repression
###########################################
# Figure: Average Political Rights Scores####
avg.over.country <- function(var,fct=mean,dat=data.sum){
  foo <- matrix(dat[,var],nrow=29) ## rows = years, columns = countries; 1985-2010 are 28 years, thus nrow=28
  foo <- apply(foo,1,fct,na.rm=TRUE)
  foo[is.nan(foo)] <- NA
  return(foo)
}

# left side figure (CIRI, PTS)
a <- avg.over.country("CIRI Phys. Integrity")
#b <- avg.over.country("CIRI Empowerment")
c1 <- avg.over.country("PTS Amnesty")

pdf("~/Dropbox/Cambridge_University/Papers in Preparation/FDI & Pol/Paper/Figs/fig_pol.pdf",width=6,height=6)  
plot(1982:2010, a,ylim=c(2,10),type="l",lwd=3,las=2,col="red",xlab="Years",ylab="Score",main="CIRI and PTS over Time")
#lines(1985:2010,b,add=T,lwd=3,col="forestgreen",lty=2) #lty dotted line
lines(1982:2010,c1,add=T,lwd=3,col="blue",lty=3)
legend("topleft",legend=c("CIRI Phys. Integrity",
                          #  "CIRI Empowerment",
                          "PTS Amnesty"),pch=19,col=c("red",
                                                      #   "forest green",
                                                      "blue"),lty=c(1,2,3))
dev.off() ## close PDF file

# right side figure (Fariss)
d <- avg.over.country("Latent Scores (Fariss)")
length(d) #29

pdf("~/Dropbox/Cambridge_University/Papers in Preparation/FDI & Pol/Paper/Figs/fig_fariss.pdf",width=6,height=6)  # change path for your computer!
plot(1982:2010,d,ylim=c(-0.5,0.75),type="l",lwd=3,las=2,col="orange",xlab="Years",ylab="",main="Latent Rights Scores by Fariss")
#legend("topleft",legend=c("Latent Scores (Fariss)"),pch=19,col=c("red"))
dev.off() ## close PDF file
# Note: The ylim was chosen based on Fariss' 2014 paper









# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #  # # # # # # # # # # # # # # # # # # # 
# Creating table: Sectoral FDI and Personal Integrity Rights Protection (incl. petrol and mining)####
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #  # # # # # # # # # # # # # # # # # # # 

# data####
data.CIRI.PHY.LDV.p <- na.omit(t.nono[,cbind("CIRI_PHYSINT","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_petrolpGDP","Lag_lognonnegUS_fdi_foodpGDP","Lag_lognonnegUS_fdi_chemicalpGDP","Lag_lognonnegUS_fdi_prim_fab_metalpGDP","Lag_lognonnegUS_fdi_machinerypGDP","Lag_lognonnegUS_fdi_electricalpGDP","Lag_lognonnegUS_fdi_transportpGDP","Lag_lognonnegUS_fdi_whole_tradepGDP","Lag_lognonnegUS_fdi_depositorypGDP","Lag_CIRI_PHYSINT","country","year")]) 
data.PTS.LDV.p  <- na.omit(t.nono[,cbind("PTS_ai_reversed","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_petrolpGDP","Lag_lognonnegUS_fdi_foodpGDP","Lag_lognonnegUS_fdi_chemicalpGDP","Lag_lognonnegUS_fdi_prim_fab_metalpGDP","Lag_lognonnegUS_fdi_machinerypGDP","Lag_lognonnegUS_fdi_electricalpGDP","Lag_lognonnegUS_fdi_transportpGDP","Lag_lognonnegUS_fdi_whole_tradepGDP","Lag_lognonnegUS_fdi_depositorypGDP","Lag_PTS_ai_reversed","country","year")])
data.Fariss.p.LDV <- na.omit(t.nono[,cbind("Fariss_HR","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_petrolpGDP","Lag_lognonnegUS_fdi_foodpGDP","Lag_lognonnegUS_fdi_chemicalpGDP","Lag_lognonnegUS_fdi_prim_fab_metalpGDP","Lag_lognonnegUS_fdi_machinerypGDP","Lag_lognonnegUS_fdi_electricalpGDP","Lag_lognonnegUS_fdi_transportpGDP","Lag_lognonnegUS_fdi_whole_tradepGDP","Lag_lognonnegUS_fdi_depositorypGDP","Lag_Fariss_HR","country","year")])
data.CIRI.PHY.LDV.m <- na.omit(t.nono[,cbind("CIRI_PHYSINT","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_miningpGDP","Lag_lognonnegUS_fdi_foodpGDP","Lag_lognonnegUS_fdi_chemicalpGDP","Lag_lognonnegUS_fdi_prim_fab_metalpGDP","Lag_lognonnegUS_fdi_machinerypGDP","Lag_lognonnegUS_fdi_electricalpGDP","Lag_lognonnegUS_fdi_transportpGDP","Lag_lognonnegUS_fdi_whole_tradepGDP","Lag_lognonnegUS_fdi_depositorypGDP","Lag_CIRI_PHYSINT","country","year")])
data.PTS.LDV.m  <- na.omit(t.nono[,cbind("PTS_ai_reversed","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_miningpGDP","Lag_lognonnegUS_fdi_foodpGDP","Lag_lognonnegUS_fdi_chemicalpGDP","Lag_lognonnegUS_fdi_prim_fab_metalpGDP","Lag_lognonnegUS_fdi_machinerypGDP","Lag_lognonnegUS_fdi_electricalpGDP","Lag_lognonnegUS_fdi_transportpGDP","Lag_lognonnegUS_fdi_whole_tradepGDP","Lag_lognonnegUS_fdi_depositorypGDP","Lag_PTS_ai_reversed","country","year")])
data.Fariss.m.LDV <- na.omit(t.nono[,cbind("Fariss_HR","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_miningpGDP","Lag_lognonnegUS_fdi_foodpGDP","Lag_lognonnegUS_fdi_chemicalpGDP","Lag_lognonnegUS_fdi_prim_fab_metalpGDP","Lag_lognonnegUS_fdi_machinerypGDP","Lag_lognonnegUS_fdi_electricalpGDP","Lag_lognonnegUS_fdi_transportpGDP","Lag_lognonnegUS_fdi_whole_tradepGDP","Lag_lognonnegUS_fdi_depositorypGDP","Lag_Fariss_HR","country","year")])

#  petrol models ####
CIRI.logit.LDV.p <- lrm(CIRI_PHYSINT ~ Lag_lognonnegUS_fdi_petrolpGDP + Lag_lognonnegUS_fdi_foodpGDP + Lag_lognonnegUS_fdi_chemicalpGDP + Lag_lognonnegUS_fdi_prim_fab_metalpGDP + Lag_lognonnegUS_fdi_machinerypGDP + Lag_lognonnegUS_fdi_electricalpGDP + Lag_lognonnegUS_fdi_transportpGDP + Lag_lognonnegUS_fdi_whole_tradepGDP + Lag_lognonnegUS_fdi_depositorypGDP                         + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_CIRI_PHYSINT,data=data.CIRI.PHY.LDV.p ,x=TRUE, y=TRUE)
CIRI.logit.LDV.p.corr <- robcov(CIRI.logit.LDV.p,data.CIRI.PHY.LDV.p$country) #robust errors 

PTS.logit.LDV.p <- lrm(PTS_ai_reversed ~ Lag_lognonnegUS_fdi_petrolpGDP + Lag_lognonnegUS_fdi_foodpGDP + Lag_lognonnegUS_fdi_chemicalpGDP + Lag_lognonnegUS_fdi_prim_fab_metalpGDP + Lag_lognonnegUS_fdi_machinerypGDP + Lag_lognonnegUS_fdi_electricalpGDP + Lag_lognonnegUS_fdi_transportpGDP + Lag_lognonnegUS_fdi_whole_tradepGDP + Lag_lognonnegUS_fdi_depositorypGDP                       + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_PTS_ai_reversed,data=data.PTS.LDV.p,x=TRUE, y=TRUE)
PTS.logit.LDV.p.corr <- robcov(PTS.logit.LDV.p,data.PTS.LDV.p$country) #robust errors 

data.Fariss.p.LDV <- as.data.frame(data.Fariss.p.LDV)
form   <- Fariss_HR ~ Lag_lognonnegUS_fdi_petrolpGDP + Lag_lognonnegUS_fdi_foodpGDP + Lag_lognonnegUS_fdi_chemicalpGDP + Lag_lognonnegUS_fdi_prim_fab_metalpGDP + Lag_lognonnegUS_fdi_machinerypGDP + Lag_lognonnegUS_fdi_electricalpGDP + Lag_lognonnegUS_fdi_transportpGDP + Lag_lognonnegUS_fdi_whole_tradepGDP + Lag_lognonnegUS_fdi_depositorypGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_Fariss_HR
res.po.p <- plm(form,model="pooling",index=c("country","year"),data=data.Fariss.p.LDV)
summary(res.po.p)      
## PCSE a la Beck-Katz
res.BKtime.p  <- vcovBK(x=res.po.p,cluster="time")
ct.time.p     <- coeftest(res.po.p,vcov=res.BKtime.p)
print(ct.time.p)

# rename lagged values to get them into one row
names(CIRI.logit.LDV.p.corr$coefficients)
names(CIRI.logit.LDV.p.corr$coefficients)[names(CIRI.logit.LDV.p.corr$coefficients) == "Lag_CIRI_PHYSINT"] <- "Past"
rownames(CIRI.logit.LDV.p.corr$var)[rownames(CIRI.logit.LDV.p.corr$var)== "Lag_CIRI_PHYSINT"] <- "Past"
colnames(CIRI.logit.LDV.p.corr$var)[colnames(CIRI.logit.LDV.p.corr$var)== "Lag_CIRI_PHYSINT"] <- "Past"
names(PTS.logit.LDV.p.corr$coefficients)
names(PTS.logit.LDV.p.corr$coefficients)[names(PTS.logit.LDV.p.corr$coefficients) == "Lag_PTS_ai_reversed"] <- "Past"
rownames(PTS.logit.LDV.p.corr$var)[rownames(PTS.logit.LDV.p.corr$var)== "Lag_PTS_ai_reversed"] <- "Past"
colnames(PTS.logit.LDV.p.corr$var)[colnames(PTS.logit.LDV.p.corr$var)== "Lag_PTS_ai_reversed"] <- "Past"
res.po.p$vcov <- res.BKtime.p  # replacing the first uncorrected residual covariance structure with the corrected one see str()
names(res.po.p$coefficients)[names(res.po.p$coefficients) == "Lag_Fariss_HR"] <- "Past"
rownames(res.po.p$vcov)[rownames(res.po.p$vcov)== "Lag_Fariss_HR"] <- "Past"
colnames(res.po.p$vcov)[colnames(res.po.p$vcov)== "Lag_Fariss_HR"] <- "Past"


#  mining models####
CIRI.logit.LDV.m <- lrm(CIRI_PHYSINT ~ Lag_lognonnegUS_fdi_miningpGDP + Lag_lognonnegUS_fdi_foodpGDP + Lag_lognonnegUS_fdi_chemicalpGDP + Lag_lognonnegUS_fdi_prim_fab_metalpGDP + Lag_lognonnegUS_fdi_machinerypGDP + Lag_lognonnegUS_fdi_electricalpGDP + Lag_lognonnegUS_fdi_transportpGDP + Lag_lognonnegUS_fdi_whole_tradepGDP + Lag_lognonnegUS_fdi_depositorypGDP                      + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_CIRI_PHYSINT,data=data.CIRI.PHY.LDV.m ,x=TRUE, y=TRUE)
CIRI.logit.LDV.m.corr <- robcov(CIRI.logit.LDV.m,data.CIRI.PHY.LDV.m$country) #robust errors 

PTS.logit.LDV.m <- lrm(PTS_ai_reversed ~ Lag_lognonnegUS_fdi_miningpGDP + Lag_lognonnegUS_fdi_foodpGDP + Lag_lognonnegUS_fdi_chemicalpGDP + Lag_lognonnegUS_fdi_prim_fab_metalpGDP + Lag_lognonnegUS_fdi_machinerypGDP + Lag_lognonnegUS_fdi_electricalpGDP + Lag_lognonnegUS_fdi_transportpGDP + Lag_lognonnegUS_fdi_whole_tradepGDP + Lag_lognonnegUS_fdi_depositorypGDP    + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_PTS_ai_reversed,data=data.PTS.LDV.m,x=TRUE, y=TRUE)
PTS.logit.LDV.m.corr <- robcov(PTS.logit.LDV.m,data.PTS.LDV.m$country) 

data.Fariss.m.LDV <- as.data.frame(data.Fariss.m.LDV)
form   <- Fariss_HR ~ Lag_lognonnegUS_fdi_miningpGDP + Lag_lognonnegUS_fdi_foodpGDP + Lag_lognonnegUS_fdi_chemicalpGDP + Lag_lognonnegUS_fdi_prim_fab_metalpGDP + Lag_lognonnegUS_fdi_machinerypGDP + Lag_lognonnegUS_fdi_electricalpGDP + Lag_lognonnegUS_fdi_transportpGDP + Lag_lognonnegUS_fdi_whole_tradepGDP + Lag_lognonnegUS_fdi_depositorypGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_Fariss_HR
res.po.m <- plm(form,model="pooling",index=c("country","year"),data=data.Fariss.m.LDV)
summary(res.po.m)      
## PCSE a la Beck-Katz
res.BKtime.m  <- vcovBK(x=res.po.m,cluster="time")
ct.time.m     <- coeftest(res.po.m,vcov=res.BKtime.m)
print(ct.time.m)

# rename lagged values to get them into one row#
names(CIRI.logit.LDV.m.corr$coefficients)
names(CIRI.logit.LDV.m.corr$coefficients)[names(CIRI.logit.LDV.m.corr$coefficients) == "Lag_CIRI_PHYSINT"] <- "Past"
rownames(CIRI.logit.LDV.m.corr$var)[rownames(CIRI.logit.LDV.m.corr$var)== "Lag_CIRI_PHYSINT"] <- "Past"
colnames(CIRI.logit.LDV.m.corr$var)[colnames(CIRI.logit.LDV.m.corr$var)== "Lag_CIRI_PHYSINT"] <- "Past"
names(PTS.logit.LDV.m.corr$coefficients)
names(PTS.logit.LDV.m.corr$coefficients)[names(PTS.logit.LDV.m.corr$coefficients) == "Lag_PTS_ai_reversed"] <- "Past"
rownames(PTS.logit.LDV.m.corr$var)[rownames(PTS.logit.LDV.m.corr$var)== "Lag_PTS_ai_reversed"] <- "Past"
colnames(PTS.logit.LDV.m.corr$var)[colnames(PTS.logit.LDV.m.corr$var)== "Lag_PTS_ai_reversed"] <- "Past"

res.po.m$vcov <- res.BKtime.m  # replacing the first uncorrected residual covariance structure with the corrected one see str()
names(res.po.m$coefficients)[names(res.po.m$coefficients) == "Lag_Fariss_HR"] <- "Past"
rownames(res.po.m$vcov)[rownames(res.po.m$vcov)== "Lag_Fariss_HR"] <- "Past"
colnames(res.po.m$vcov)[colnames(res.po.m$vcov)== "Lag_Fariss_HR"] <- "Past"

# Large table with petrol & mining ###
varnames <- c("Petrol FDI","Mining FDI","Food FDI","Chemical FDI","Metal FDI","Machinery FDI","Electrical FDI","Transport FDI", "Wholesale FDI","Depository FDI", "Trade", "GDP p. capita", "Population", "Democracy", "Conflict", "Past")
dvnames <- c("CIRI Integrity","PTS Amnesty","Latent Scores","CIRI Integrity","PTS Amnesty","Latent Scores")
stargazer(CIRI.logit.LDV.p.corr,PTS.logit.LDV.p.corr,res.po.p,CIRI.logit.LDV.m.corr,PTS.logit.LDV.m.corr,res.po.m, 
          covariate.labels=varnames, dep.var.labels=dvnames, 
          omit=c("y>=1","y>=2","y>=3","y>=4","y>=5","y>=6","y>=7","y>=8","factor","Constant","(Intercept)"), 
          omit.stat = c("f","ser","chi2"),
          nobs=T,title="Sectoral FDI and Personal Integrity Rights (incl. petrol and mining)",no.space=T,font.size="scriptsize", notes=c("CIRI and PTS: Ordered logit with country-clustered standard errors in parentheses below the coefficient estimates.","Latent Scores: OLS with panel corrected standard errors (PCSE) in parentheses below the coefficient estimates.","All FDI is divided by GDP, then logged. GDP p. capita, population and trade are logged.","Past is the lagged dependent variable (LDV)."), add.lines = list(c("Years", "1983-1999", "1983-1999","1983-1999","2000-2010","2000-2010","2000-2010")),notes.align="l",type="text",label="Sectoral.tex",out="Sectoral.tex")












# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 
# creating table "U.S. FDI and Personal Integrity Rights Protection (incl. U.S. Dominance)" ####
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 

# create variable for US FDI dominance ####
m <- t.nono
dim(m)
m$"USdominance" <- round(m$nonnegUS_fdi_total/m$UN_FDI_stock*100,2)

table(is.nan(m$USdominance)) #TRUE 3
table(is.infinite(m$USdominance)) #TRUE 25
# replace NaN and infinite values with NA
m$USdominance[ is.infinite(m$USdominance) ] <- NA #you can replace NaN with NA like this http://stackoverflow.com/questions/21320655/dealing-with-nan-when-calculating-means
m$USdominance[ is.na(m$USdominance) ] <- NA

table(is.nan(m$USdominance)) #TRUE 0
table(is.na(m$USdominance)) #TRUE 2165
table(is.infinite(m$USdominance)) #TRUE 0

# create dummy
m$"USdominance_dummy50" <- NA
m$USdominance_dummy50[m$USdominance > 50] <- 1
m$USdominance_dummy50[m$USdominance <= 50] <- 0   

# some US Dominance values are above 100%, indicating that the value for U.S. FDI may be higher than that for global FDI (often due to small classification differences). I re-set these values back to 100. (Note: If I don't set them back to 100, the results are practically the same as the table I'm going to produce now; signs and coefficients are the same except a few cases where the third decimal might change from e.g. 0.006 to 0.007.). 

t.nono$"Lag_USdominanceCut" <- t.nono$Lag_USdominance
t.nono$"Lag_USdominanceCut"[t.nono$Lag_USdominance > 100] <- 100
summary(t.nono$Lag_USdominance)
summary(t.nono$Lag_USdominanceCut)

# Create data sets US FDI total and sectoral, refined dominance and dummy in one ####
data.CIRI.PHY.LDV <- na.omit(t.nono[,cbind("CIRI_PHYSINT","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_totalpGDP","Lag_CIRI_PHYSINT", "Lag_USdominanceCut","Lag_USdominance_dummy50","country","year")])
data.PTS.LDV <- na.omit(t.nono[,cbind("PTS_ai_reversed","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_totalpGDP", "Lag_PTS_ai_reversed", "Lag_USdominanceCut","Lag_USdominance_dummy50", "country","year")])
data.Fariss.LDV <- na.omit(t.nono[,cbind("Fariss_HR","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_totalpGDP", "Lag_Fariss_HR","Lag_USdominanceCut","Lag_USdominance_dummy50","country","year")]) 
data.Fariss.LDV$country <- factor(as.character(data.Fariss.LDV$country)) 

data.CIRI.PHY.excl.LDV <- na.omit(t.nono[,cbind("CIRI_PHYSINT","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_foodpGDP","Lag_lognonnegUS_fdi_chemicalpGDP","Lag_lognonnegUS_fdi_prim_fab_metalpGDP","Lag_lognonnegUS_fdi_machinerypGDP","Lag_lognonnegUS_fdi_electricalpGDP","Lag_lognonnegUS_fdi_transportpGDP","Lag_lognonnegUS_fdi_whole_tradepGDP","Lag_lognonnegUS_fdi_depositorypGDP","Lag_USdominanceCut","Lag_USdominance_dummy50","Lag_CIRI_PHYSINT","country","year")])
data.PTS.excl.LDV  <- na.omit(t.nono[,cbind("PTS_ai_reversed","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_foodpGDP","Lag_lognonnegUS_fdi_chemicalpGDP","Lag_lognonnegUS_fdi_prim_fab_metalpGDP","Lag_lognonnegUS_fdi_machinerypGDP","Lag_lognonnegUS_fdi_electricalpGDP","Lag_lognonnegUS_fdi_transportpGDP","Lag_lognonnegUS_fdi_whole_tradepGDP","Lag_lognonnegUS_fdi_depositorypGDP","Lag_USdominanceCut","Lag_USdominance_dummy50",
                                            "Lag_PTS_ai_reversed" ,"country","year")])
data.Fariss.excl.LDV <- na.omit(t.nono[,cbind("Fariss_HR","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_foodpGDP","Lag_lognonnegUS_fdi_chemicalpGDP","Lag_lognonnegUS_fdi_prim_fab_metalpGDP","Lag_lognonnegUS_fdi_machinerypGDP","Lag_lognonnegUS_fdi_electricalpGDP","Lag_lognonnegUS_fdi_transportpGDP","Lag_lognonnegUS_fdi_whole_tradepGDP","Lag_lognonnegUS_fdi_depositorypGDP","Lag_USdominanceCut","Lag_USdominance_dummy50",  "Lag_Fariss_HR","country","year")])

#?models with refined dominance ####
CIRI.logit.LDV <- lrm(CIRI_PHYSINT ~ Lag_lognonnegUS_fdi_totalpGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_USdominanceCut + Lag_CIRI_PHYSINT,data=data.CIRI.PHY.LDV,x=TRUE, y=TRUE)
CIRI.logit.LDV.corr <- robcov(CIRI.logit.LDV,data.CIRI.PHY.LDV$country) #robust errors  

PTS.logit.LDV <- lrm(PTS_ai_reversed ~ Lag_lognonnegUS_fdi_totalpGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_USdominanceCut + Lag_PTS_ai_reversed,data=data.PTS.LDV,x=TRUE, y=TRUE)
PTS.logit.LDV.corr <- robcov(PTS.logit.LDV,data.PTS.LDV$country) #robust errors  

data.Fariss.LDV <- as.data.frame(data.Fariss.LDV)
form   <- Fariss_HR ~ Lag_lognonnegUS_fdi_totalpGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_USdominanceCut + Lag_Fariss_HR
res.po <- plm(form,model="pooling",index=c("country","year"),data=data.Fariss.LDV)
summary(res.po)      
## PCSE a la Beck-Katz
res.BKtime  <- vcovBK(x=res.po,cluster="time")
ct.time     <- coeftest(res.po,vcov=res.BKtime)
print(ct.time)

# rename lagged values to get them into one row with stargazer; replace plm se's with corrected ones
names(CIRI.logit.LDV.corr$coefficients)
names(CIRI.logit.LDV.corr$coefficients)[names(CIRI.logit.LDV.corr$coefficients) == "Lag_CIRI_PHYSINT"] <- "Past"
rownames(CIRI.logit.LDV.corr$var)[rownames(CIRI.logit.LDV.corr$var)== "Lag_CIRI_PHYSINT"] <- "Past"
colnames(CIRI.logit.LDV.corr$var)[colnames(CIRI.logit.LDV.corr$var)== "Lag_CIRI_PHYSINT"] <- "Past"

names(PTS.logit.LDV.corr$coefficients)
names(PTS.logit.LDV.corr$coefficients)[names(PTS.logit.LDV.corr$coefficients) == "Lag_PTS_ai_reversed"] <- "Past"
rownames(PTS.logit.LDV.corr$var)[rownames(PTS.logit.LDV.corr$var)== "Lag_PTS_ai_reversed"] <- "Past"
colnames(PTS.logit.LDV.corr$var)[colnames(PTS.logit.LDV.corr$var)== "Lag_PTS_ai_reversed"] <- "Past"

res.po$vcov <- res.BKtime  # replacing the first uncorrected residual covariance structure with the corrected one see str()
names(res.po$coefficients)[names(res.po$coefficients) == "Lag_Fariss_HR"] <- "Past"
rownames(res.po$vcov)[rownames(res.po$vcov)== "Lag_Fariss_HR"] <- "Past"
colnames(res.po$vcov)[colnames(res.po$vcov)== "Lag_Fariss_HR"] <- "Past"

CIRI.logit.LDV.excl <- lrm(CIRI_PHYSINT ~ Lag_lognonnegUS_fdi_foodpGDP + Lag_lognonnegUS_fdi_chemicalpGDP + Lag_lognonnegUS_fdi_prim_fab_metalpGDP + Lag_lognonnegUS_fdi_machinerypGDP + Lag_lognonnegUS_fdi_electricalpGDP + Lag_lognonnegUS_fdi_transportpGDP + Lag_lognonnegUS_fdi_whole_tradepGDP + Lag_lognonnegUS_fdi_depositorypGDP+ Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl +  Lag_USdominanceCut + Lag_CIRI_PHYSINT,data=data.CIRI.PHY.excl.LDV  ,x=TRUE, y=TRUE)
CIRI.logit.LDV.excl.corr <- robcov(CIRI.logit.LDV.excl,data.CIRI.PHY.excl.LDV$country) #robust errors  

PTS.logit.LDV.excl <- lrm(PTS_ai_reversed ~ Lag_lognonnegUS_fdi_foodpGDP + Lag_lognonnegUS_fdi_chemicalpGDP + Lag_lognonnegUS_fdi_prim_fab_metalpGDP + Lag_lognonnegUS_fdi_machinerypGDP + Lag_lognonnegUS_fdi_electricalpGDP + Lag_lognonnegUS_fdi_transportpGDP + Lag_lognonnegUS_fdi_whole_tradepGDP + Lag_lognonnegUS_fdi_depositorypGDP           + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl  + Lag_USdominanceCut + Lag_PTS_ai_reversed,data=data.PTS.excl.LDV,x=TRUE, y=TRUE)
PTS.logit.LDV.excl.corr <- robcov(PTS.logit.LDV.excl,data.PTS.excl.LDV$country) 

data.Fariss.excl.LDV <- as.data.frame(data.Fariss.excl.LDV)
form   <- Fariss_HR ~ Lag_lognonnegUS_fdi_foodpGDP + Lag_lognonnegUS_fdi_chemicalpGDP + Lag_lognonnegUS_fdi_prim_fab_metalpGDP + Lag_lognonnegUS_fdi_machinerypGDP + Lag_lognonnegUS_fdi_electricalpGDP + Lag_lognonnegUS_fdi_transportpGDP + Lag_lognonnegUS_fdi_whole_tradepGDP + Lag_lognonnegUS_fdi_depositorypGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl  + + Lag_USdominanceCut + Lag_Fariss_HR
res.po.excl <- plm(form,model="pooling",index=c("country","year"),data=data.Fariss.excl.LDV)
summary(res.po.excl)      
## PCSE a la Beck-Katz
res.BKtime.excl  <- vcovBK(x=res.po.excl,cluster="time")
ct.time.excl     <- coeftest(res.po.excl,vcov=res.BKtime.excl)
print(ct.time.excl)

# rename lagged values to get them into one row
names(CIRI.logit.LDV.excl.corr$coefficients)
names(CIRI.logit.LDV.excl.corr$coefficients)[names(CIRI.logit.LDV.excl.corr$coefficients) == "Lag_CIRI_PHYSINT"] <- "Past"
rownames(CIRI.logit.LDV.excl.corr$var)[rownames(CIRI.logit.LDV.excl.corr$var)== "Lag_CIRI_PHYSINT"] <- "Past"
colnames(CIRI.logit.LDV.excl.corr$var)[colnames(CIRI.logit.LDV.excl.corr$var)== "Lag_CIRI_PHYSINT"] <- "Past"
names(PTS.logit.LDV.excl.corr$coefficients)
names(PTS.logit.LDV.excl.corr$coefficients)[names(PTS.logit.LDV.excl.corr$coefficients) == "Lag_PTS_ai_reversed"] <- "Past"
rownames(PTS.logit.LDV.excl.corr$var)[rownames(PTS.logit.LDV.excl.corr$var)== "Lag_PTS_ai_reversed"] <- "Past"
colnames(PTS.logit.LDV.excl.corr$var)[colnames(PTS.logit.LDV.excl.corr$var)== "Lag_PTS_ai_reversed"] <- "Past"

res.po.excl$vcov <- res.BKtime.excl  # replacing the first uncorrected residual covariance structure with the corrected one see str()
names(res.po.excl$coefficients)[names(res.po.excl$coefficients) == "Lag_Fariss_HR"] <- "Past"
rownames(res.po.excl$vcov)[rownames(res.po.excl$vcov)== "Lag_Fariss_HR"] <- "Past"
colnames(res.po.excl$vcov)[colnames(res.po.excl$vcov)== "Lag_Fariss_HR"] <- "Past"

# res table sectoral refined dominance interactions #### 
varnames <- c("US total FDI","Food FDI","Chemical FDI","Metal FDI","Machinery FDI","Electrical FDI","Transport FDI", "Whole. Trade FDI","Depository FDI", "Trade", "GDP p. capita", "Population", "Democracy", "Conflict", "US FDI Dominance","Past")
dvnames <- c("CIRI Integrity","PTS Amnesty","Latent Scores","CIRI Integrity","PTS Amnesty","Latent Scores")

stargazer(CIRI.logit.LDV.corr,PTS.logit.LDV.corr,res.po,
          CIRI.logit.LDV.excl.corr,PTS.logit.LDV.excl.corr,res.po.excl, 
          covariate.labels=varnames, 
          dep.var.labels=dvnames, 
          omit=c("y>=1","y>=2","y>=3","y>=4","y>=5","y>=6","y>=7","y>=8","factor","Constant","(Intercept)"), 
          omit.stat = c("f","ser","chi2"),
          nobs=T,title="Total and Sectoral FDI and Personal Integrity Rights, incl. U.S. Dominance",no.space=T,font.size="scriptsize", notes=c("Ordered logit with country-clustered standard errors. OLS with panel corrected standard errors.","U.S. Dominance is the share of US FDI in Global FDI.","All FDI is divided by GDP, then logged. GDP p. capita, population and trade are logged.","Past is the lagged dependent variable (LDV). Years included: 1983-2010."),notes.align="l",type="text",label="revisions.USdominanceCut.tex",out="revisions.USdominanceCut.tex")




# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 
# The following table is NOT in the paper or appendix:
# "U.S. FDI and Personal Integrity Rights Protection (incl. U.S. Dominance, but without re-setting the maximum values to 100)
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 

# create lagged version of US dominance
t.nono <- m
tobelagged <- c("USdominance","USdominance_dummy50") #make sure this is USdominance
d2 <- my.lag.vars(t.nono,vars=tobelagged)
dim(t.nono) #4582  40
dim(d2) #4582  42
t.nono <- d2 #rename so that we can use t.nono from now on

# Create data sets US FDI total and sectoral, refined dominance and dummy in one (same obsnr, so one data set is ok) ####
data.CIRI.PHY.LDV <- na.omit(t.nono[,cbind("CIRI_PHYSINT","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_totalpGDP","Lag_CIRI_PHYSINT", "Lag_USdominance","Lag_USdominance_dummy50","country","year")])
data.PTS.LDV <- na.omit(t.nono[,cbind("PTS_ai_reversed","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_totalpGDP", "Lag_PTS_ai_reversed", "Lag_USdominance","Lag_USdominance_dummy50", "country","year")])
data.Fariss.LDV <- na.omit(t.nono[,cbind("Fariss_HR","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_totalpGDP", "Lag_Fariss_HR","Lag_USdominance","Lag_USdominance_dummy50","country","year")]) 
data.Fariss.LDV$country <- factor(as.character(data.Fariss.LDV$country)) 

data.CIRI.PHY.excl.LDV <- na.omit(t.nono[,cbind("CIRI_PHYSINT","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_foodpGDP","Lag_lognonnegUS_fdi_chemicalpGDP","Lag_lognonnegUS_fdi_prim_fab_metalpGDP","Lag_lognonnegUS_fdi_machinerypGDP","Lag_lognonnegUS_fdi_electricalpGDP","Lag_lognonnegUS_fdi_transportpGDP","Lag_lognonnegUS_fdi_whole_tradepGDP","Lag_lognonnegUS_fdi_depositorypGDP","Lag_USdominance","Lag_USdominance_dummy50","Lag_CIRI_PHYSINT","country","year")])
data.PTS.excl.LDV  <- na.omit(t.nono[,cbind("PTS_ai_reversed","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_foodpGDP","Lag_lognonnegUS_fdi_chemicalpGDP","Lag_lognonnegUS_fdi_prim_fab_metalpGDP","Lag_lognonnegUS_fdi_machinerypGDP","Lag_lognonnegUS_fdi_electricalpGDP","Lag_lognonnegUS_fdi_transportpGDP","Lag_lognonnegUS_fdi_whole_tradepGDP","Lag_lognonnegUS_fdi_depositorypGDP","Lag_USdominance","Lag_USdominance_dummy50",
                                            "Lag_PTS_ai_reversed" ,"country","year")])

data.Fariss.excl.LDV <- na.omit(t.nono[,cbind("Fariss_HR","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_foodpGDP","Lag_lognonnegUS_fdi_chemicalpGDP","Lag_lognonnegUS_fdi_prim_fab_metalpGDP","Lag_lognonnegUS_fdi_machinerypGDP","Lag_lognonnegUS_fdi_electricalpGDP","Lag_lognonnegUS_fdi_transportpGDP","Lag_lognonnegUS_fdi_whole_tradepGDP","Lag_lognonnegUS_fdi_depositorypGDP","Lag_USdominance","Lag_USdominance_dummy50",  "Lag_Fariss_HR","country","year")])

# models with refined dominance ####
CIRI.logit.LDV <- lrm(CIRI_PHYSINT ~ Lag_lognonnegUS_fdi_totalpGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_USdominance + Lag_CIRI_PHYSINT,data=data.CIRI.PHY.LDV,x=TRUE, y=TRUE)
CIRI.logit.LDV.corr <- robcov(CIRI.logit.LDV,data.CIRI.PHY.LDV$country) #robust errors 

PTS.logit.LDV <- lrm(PTS_ai_reversed ~ Lag_lognonnegUS_fdi_totalpGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_USdominance + Lag_PTS_ai_reversed,data=data.PTS.LDV,x=TRUE, y=TRUE)
PTS.logit.LDV.corr <- robcov(PTS.logit.LDV,data.PTS.LDV$country) #robust errors 

data.Fariss.LDV <- as.data.frame(data.Fariss.LDV)
form   <- Fariss_HR ~ Lag_lognonnegUS_fdi_totalpGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_USdominance + Lag_Fariss_HR
res.po <- plm(form,model="pooling",index=c("country","year"),data=data.Fariss.LDV)
summary(res.po)      
## PCSE a la Beck-Katz
res.BKtime  <- vcovBK(x=res.po,cluster="time")
ct.time     <- coeftest(res.po,vcov=res.BKtime)
print(ct.time)

# rename lagged values to get them into one row with stargazer; replace plm se's with corrected ones
names(CIRI.logit.LDV.corr$coefficients)
names(CIRI.logit.LDV.corr$coefficients)[names(CIRI.logit.LDV.corr$coefficients) == "Lag_CIRI_PHYSINT"] <- "Past"
rownames(CIRI.logit.LDV.corr$var)[rownames(CIRI.logit.LDV.corr$var)== "Lag_CIRI_PHYSINT"] <- "Past"
colnames(CIRI.logit.LDV.corr$var)[colnames(CIRI.logit.LDV.corr$var)== "Lag_CIRI_PHYSINT"] <- "Past"

names(PTS.logit.LDV.corr$coefficients)
names(PTS.logit.LDV.corr$coefficients)[names(PTS.logit.LDV.corr$coefficients) == "Lag_PTS_ai_reversed"] <- "Past"
rownames(PTS.logit.LDV.corr$var)[rownames(PTS.logit.LDV.corr$var)== "Lag_PTS_ai_reversed"] <- "Past"
colnames(PTS.logit.LDV.corr$var)[colnames(PTS.logit.LDV.corr$var)== "Lag_PTS_ai_reversed"] <- "Past"

res.po$vcov <- res.BKtime  # replacing the first uncorrected residual covariance structure with the corrected one see str()
names(res.po$coefficients)[names(res.po$coefficients) == "Lag_Fariss_HR"] <- "Past"
rownames(res.po$vcov)[rownames(res.po$vcov)== "Lag_Fariss_HR"] <- "Past"
colnames(res.po$vcov)[colnames(res.po$vcov)== "Lag_Fariss_HR"] <- "Past"

CIRI.logit.LDV.excl <- lrm(CIRI_PHYSINT ~ Lag_lognonnegUS_fdi_foodpGDP + Lag_lognonnegUS_fdi_chemicalpGDP + Lag_lognonnegUS_fdi_prim_fab_metalpGDP + Lag_lognonnegUS_fdi_machinerypGDP + Lag_lognonnegUS_fdi_electricalpGDP + Lag_lognonnegUS_fdi_transportpGDP + Lag_lognonnegUS_fdi_whole_tradepGDP + Lag_lognonnegUS_fdi_depositorypGDP+ Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + + Lag_USdominance + Lag_CIRI_PHYSINT,data=data.CIRI.PHY.excl.LDV  ,x=TRUE, y=TRUE)
CIRI.logit.LDV.excl.corr <- robcov(CIRI.logit.LDV.excl,data.CIRI.PHY.excl.LDV$country) #robust errors  

PTS.logit.LDV.excl <- lrm(PTS_ai_reversed ~ Lag_lognonnegUS_fdi_foodpGDP + Lag_lognonnegUS_fdi_chemicalpGDP + Lag_lognonnegUS_fdi_prim_fab_metalpGDP + Lag_lognonnegUS_fdi_machinerypGDP + Lag_lognonnegUS_fdi_electricalpGDP + Lag_lognonnegUS_fdi_transportpGDP + Lag_lognonnegUS_fdi_whole_tradepGDP + Lag_lognonnegUS_fdi_depositorypGDP           + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl  + Lag_USdominance + Lag_PTS_ai_reversed,data=data.PTS.excl.LDV,x=TRUE, y=TRUE)
PTS.logit.LDV.excl.corr <- robcov(PTS.logit.LDV.excl,data.PTS.excl.LDV$country) 

data.Fariss.excl.LDV <- as.data.frame(data.Fariss.excl.LDV)
form   <- Fariss_HR ~ Lag_lognonnegUS_fdi_foodpGDP + Lag_lognonnegUS_fdi_chemicalpGDP + Lag_lognonnegUS_fdi_prim_fab_metalpGDP + Lag_lognonnegUS_fdi_machinerypGDP + Lag_lognonnegUS_fdi_electricalpGDP + Lag_lognonnegUS_fdi_transportpGDP + Lag_lognonnegUS_fdi_whole_tradepGDP + Lag_lognonnegUS_fdi_depositorypGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl  + + Lag_USdominance + Lag_Fariss_HR
res.po.excl <- plm(form,model="pooling",index=c("country","year"),data=data.Fariss.excl.LDV)
summary(res.po.excl)      
## PCSE a la Beck-Katz
res.BKtime.excl  <- vcovBK(x=res.po.excl,cluster="time")
ct.time.excl     <- coeftest(res.po.excl,vcov=res.BKtime.excl)
print(ct.time.excl)

# rename lagged values to get them into one row
names(CIRI.logit.LDV.excl.corr$coefficients)
names(CIRI.logit.LDV.excl.corr$coefficients)[names(CIRI.logit.LDV.excl.corr$coefficients) == "Lag_CIRI_PHYSINT"] <- "Past"
rownames(CIRI.logit.LDV.excl.corr$var)[rownames(CIRI.logit.LDV.excl.corr$var)== "Lag_CIRI_PHYSINT"] <- "Past"
colnames(CIRI.logit.LDV.excl.corr$var)[colnames(CIRI.logit.LDV.excl.corr$var)== "Lag_CIRI_PHYSINT"] <- "Past"
names(PTS.logit.LDV.excl.corr$coefficients)
names(PTS.logit.LDV.excl.corr$coefficients)[names(PTS.logit.LDV.excl.corr$coefficients) == "Lag_PTS_ai_reversed"] <- "Past"
rownames(PTS.logit.LDV.excl.corr$var)[rownames(PTS.logit.LDV.excl.corr$var)== "Lag_PTS_ai_reversed"] <- "Past"
colnames(PTS.logit.LDV.excl.corr$var)[colnames(PTS.logit.LDV.excl.corr$var)== "Lag_PTS_ai_reversed"] <- "Past"

res.po.excl$vcov <- res.BKtime.excl  # replacing the first uncorrected residual covariance structure with the corrected one see str()
names(res.po.excl$coefficients)[names(res.po.excl$coefficients) == "Lag_Fariss_HR"] <- "Past"
rownames(res.po.excl$vcov)[rownames(res.po.excl$vcov)== "Lag_Fariss_HR"] <- "Past"
colnames(res.po.excl$vcov)[colnames(res.po.excl$vcov)== "Lag_Fariss_HR"] <- "Past"

# res table sectoral refined dominance interactions #### 
varnames <- c("US total FDI","Food FDI","Chemical FDI","Metal FDI","Machinery FDI","Electrical FDI","Transport FDI", "Whole. Trade FDI","Depository FDI", "Trade", "GDP p. capita", "Population", "Democracy", "Conflict", "US FDI Dominance","Past")
dvnames <- c("CIRI Integrity","PTS Amnesty","Latent Scores","CIRI Integrity","PTS Amnesty","Latent Scores")

stargazer(CIRI.logit.LDV.corr,PTS.logit.LDV.corr,res.po,
          CIRI.logit.LDV.excl.corr,PTS.logit.LDV.excl.corr,res.po.excl, 
          covariate.labels=varnames, 
          dep.var.labels=dvnames, 
          omit=c("y>=1","y>=2","y>=3","y>=4","y>=5","y>=6","y>=7","y>=8","factor","Constant","(Intercept)"), 
          omit.stat = c("f","ser","chi2"),
          nobs=T,title="Total and Sectoral FDI and Personal Integrity Rights, incl. U.S. Dominance",no.space=T,font.size="scriptsize", notes=c("Ordered logit with country-clustered standard errors. OLS with panel corrected standard errors.","U.S. Dominance is the share of US FDI in Global FDI.","All FDI is divided by GDP, then logged. GDP p. capita, population and trade are logged.","Past is the lagged dependent variable (LDV). Years included: 1983-2010."),notes.align="l",type="text",label="revisions.USdominance.tex",out="revisions.USdominance.tex") 

##### Note that the table "revisions.USdominance.tex" is not in the paper! See comments above. This is just to demonstrate that cutting off values ofor US dominance over 100 versus not doing it did not change the results substantially.#####





# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 
# Creating table "U.S. FDI and Personal Integrity Rights Protection (incl. U.S. Dominance Dummy)" ####
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 

CIRI.logit.LDV <- lrm(CIRI_PHYSINT ~ Lag_lognonnegUS_fdi_totalpGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_USdominance_dummy50 + Lag_CIRI_PHYSINT,data=data.CIRI.PHY.LDV,x=TRUE, y=TRUE)
CIRI.logit.LDV.corr <- robcov(CIRI.logit.LDV,data.CIRI.PHY.LDV$country) #robust errors

PTS.logit.LDV <- lrm(PTS_ai_reversed ~ Lag_lognonnegUS_fdi_totalpGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_USdominance_dummy50 + Lag_PTS_ai_reversed,data=data.PTS.LDV,x=TRUE, y=TRUE)
PTS.logit.LDV.corr <- robcov(PTS.logit.LDV,data.PTS.LDV$country) #robust errors 

data.Fariss.LDV <- as.data.frame(data.Fariss.LDV)
form   <- Fariss_HR ~ Lag_lognonnegUS_fdi_totalpGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_USdominance_dummy50 + Lag_Fariss_HR
res.po <- plm(form,model="pooling",index=c("country","year"),data=data.Fariss.LDV)
summary(res.po)      
## PCSE a la Beck-Katz
res.BKtime  <- vcovBK(x=res.po,cluster="time")
ct.time     <- coeftest(res.po,vcov=res.BKtime)
print(ct.time)

# rename lagged values to get them into one row with stargazer; replace plm se's with corrected ones
names(CIRI.logit.LDV.corr$coefficients)
names(CIRI.logit.LDV.corr$coefficients)[names(CIRI.logit.LDV.corr$coefficients) == "Lag_CIRI_PHYSINT"] <- "Past"
rownames(CIRI.logit.LDV.corr$var)[rownames(CIRI.logit.LDV.corr$var)== "Lag_CIRI_PHYSINT"] <- "Past"
colnames(CIRI.logit.LDV.corr$var)[colnames(CIRI.logit.LDV.corr$var)== "Lag_CIRI_PHYSINT"] <- "Past"

names(PTS.logit.LDV.corr$coefficients)
names(PTS.logit.LDV.corr$coefficients)[names(PTS.logit.LDV.corr$coefficients) == "Lag_PTS_ai_reversed"] <- "Past"
rownames(PTS.logit.LDV.corr$var)[rownames(PTS.logit.LDV.corr$var)== "Lag_PTS_ai_reversed"] <- "Past"
colnames(PTS.logit.LDV.corr$var)[colnames(PTS.logit.LDV.corr$var)== "Lag_PTS_ai_reversed"] <- "Past"

res.po$vcov <- res.BKtime  # replacing the first uncorrected residual covariance structure with the corrected one see str()
names(res.po$coefficients)[names(res.po$coefficients) == "Lag_Fariss_HR"] <- "Past"
rownames(res.po$vcov)[rownames(res.po$vcov)== "Lag_Fariss_HR"] <- "Past"
colnames(res.po$vcov)[colnames(res.po$vcov)== "Lag_Fariss_HR"] <- "Past"

CIRI.logit.LDV.excl <- lrm(CIRI_PHYSINT ~ Lag_lognonnegUS_fdi_foodpGDP + Lag_lognonnegUS_fdi_chemicalpGDP + Lag_lognonnegUS_fdi_prim_fab_metalpGDP + Lag_lognonnegUS_fdi_machinerypGDP + Lag_lognonnegUS_fdi_electricalpGDP + Lag_lognonnegUS_fdi_transportpGDP + Lag_lognonnegUS_fdi_whole_tradepGDP + Lag_lognonnegUS_fdi_depositorypGDP+ Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_USdominance_dummy50 + Lag_CIRI_PHYSINT,data=data.CIRI.PHY.excl.LDV  ,x=TRUE, y=TRUE)
CIRI.logit.LDV.excl.corr <- robcov(CIRI.logit.LDV.excl,data.CIRI.PHY.excl.LDV$country) #robust errors  

PTS.logit.LDV.excl <- lrm(PTS_ai_reversed ~ Lag_lognonnegUS_fdi_foodpGDP + Lag_lognonnegUS_fdi_chemicalpGDP + Lag_lognonnegUS_fdi_prim_fab_metalpGDP + Lag_lognonnegUS_fdi_machinerypGDP + Lag_lognonnegUS_fdi_electricalpGDP + Lag_lognonnegUS_fdi_transportpGDP + Lag_lognonnegUS_fdi_whole_tradepGDP + Lag_lognonnegUS_fdi_depositorypGDP           + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl  + Lag_USdominance_dummy50 + Lag_PTS_ai_reversed,data=data.PTS.excl.LDV,x=TRUE, y=TRUE)
PTS.logit.LDV.excl.corr <- robcov(PTS.logit.LDV.excl,data.PTS.excl.LDV$country) 

data.Fariss.excl.LDV <- as.data.frame(data.Fariss.excl.LDV)
form   <- Fariss_HR ~ Lag_lognonnegUS_fdi_foodpGDP + Lag_lognonnegUS_fdi_chemicalpGDP + Lag_lognonnegUS_fdi_prim_fab_metalpGDP + Lag_lognonnegUS_fdi_machinerypGDP + Lag_lognonnegUS_fdi_electricalpGDP + Lag_lognonnegUS_fdi_transportpGDP + Lag_lognonnegUS_fdi_whole_tradepGDP + Lag_lognonnegUS_fdi_depositorypGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl  + Lag_USdominance_dummy50 + Lag_Fariss_HR
res.po.excl <- plm(form,model="pooling",index=c("country","year"),data=data.Fariss.excl.LDV)
summary(res.po.excl)      
## PCSE a la Beck-Katz
res.BKtime.excl  <- vcovBK(x=res.po.excl,cluster="time")
ct.time.excl     <- coeftest(res.po.excl,vcov=res.BKtime.excl)
print(ct.time.excl)

# rename lagged values to get them into one row
names(CIRI.logit.LDV.excl.corr$coefficients)
names(CIRI.logit.LDV.excl.corr$coefficients)[names(CIRI.logit.LDV.excl.corr$coefficients) == "Lag_CIRI_PHYSINT"] <- "Past"
rownames(CIRI.logit.LDV.excl.corr$var)[rownames(CIRI.logit.LDV.excl.corr$var)== "Lag_CIRI_PHYSINT"] <- "Past"
colnames(CIRI.logit.LDV.excl.corr$var)[colnames(CIRI.logit.LDV.excl.corr$var)== "Lag_CIRI_PHYSINT"] <- "Past"
names(PTS.logit.LDV.excl.corr$coefficients)
names(PTS.logit.LDV.excl.corr$coefficients)[names(PTS.logit.LDV.excl.corr$coefficients) == "Lag_PTS_ai_reversed"] <- "Past"
rownames(PTS.logit.LDV.excl.corr$var)[rownames(PTS.logit.LDV.excl.corr$var)== "Lag_PTS_ai_reversed"] <- "Past"
colnames(PTS.logit.LDV.excl.corr$var)[colnames(PTS.logit.LDV.excl.corr$var)== "Lag_PTS_ai_reversed"] <- "Past"

res.po.excl$vcov <- res.BKtime.excl  # replacing the first uncorrected residual covariance structure with the corrected one see str()
names(res.po.excl$coefficients)[names(res.po.excl$coefficients) == "Lag_Fariss_HR"] <- "Past"
rownames(res.po.excl$vcov)[rownames(res.po.excl$vcov)== "Lag_Fariss_HR"] <- "Past"
colnames(res.po.excl$vcov)[colnames(res.po.excl$vcov)== "Lag_Fariss_HR"] <- "Past"

# res table sectoral dummy dominance interactions #### 
varnames <- c("US total FDI","Food FDI","Chemical FDI","Metal FDI","Machinery FDI","Electrical FDI","Transport FDI", "Whole. Trade FDI","Depository FDI", "Trade", "GDP p. capita", "Population", "Democracy", "Conflict", "US Dominance Dummy","Past")
dvnames <- c("CIRI Integrity","PTS Amnesty","Latent Scores","CIRI Integrity","PTS Amnesty","Latent Scores")

stargazer(CIRI.logit.LDV.corr,PTS.logit.LDV.corr,res.po,
          CIRI.logit.LDV.excl.corr,PTS.logit.LDV.excl.corr,res.po.excl, 
          covariate.labels=varnames, 
          dep.var.labels=dvnames, 
          omit=c("y>=1","y>=2","y>=3","y>=4","y>=5","y>=6","y>=7","y>=8","factor","Constant","(Intercept)"), 
          omit.stat = c("f","ser","chi2"),
          nobs=T,title="Total and Sectoral FDI and Personal Integrity Rights, incl. U.S. Dominance Dummy",no.space=T,font.size="scriptsize", notes=c("Ordered logit with country-clustered standard errors. OLS with panel corrected standard errors.","U.S. Dominance Dummy is 1 when the share of US FDI in Global FDI >0.5.","All FDI is divided by GDP, then logged. GDP p. capita, population and trade are logged.","Past is the lagged dependent variable (LDV). Years included: 1983-2010."),notes.align="l",type="text",label="revisions.USdominance.dummy.tex",out="revisions.USdominance.dummy.tex")



 
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 
# Creating table "Using Alternative Versions of the Political Terror Scale" ####
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 

colnames(t.nono)
summary(t.nono$PTS_ai_reversed)      #NAs: 1392
summary(t.nono$PTS_DoS_reversed)          # NAs:     783
t.nono$newPTS_reversed <- (t.nono$PTS_ai_reversed+t.nono$PTS_DoS_reversed)/2
summary(t.nono$newPTS_reversed) #1410
table(t.nono$newPTS_reversed)
tobelagged <- "newPTS_reversed" #make sure this is USdominance
d2 <- my.lag.vars(t.nono,vars=tobelagged)
dim(t.nono) 
dim(d2)
t.nono <- d2

# data for newPTS_reversed models
data.PTS.LDV <- na.omit(t.nono[,cbind("newPTS_reversed","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_totalpGDP", "Lag_newPTS_reversed", "country","year")])
data.PTS1.LDV <- na.omit(t.nono[,cbind("newPTS_reversed","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_log_UN_FDI_stock_pgdp","Lag_newPTS_reversed", "country","year")])
data.PTS.excl.LDV  <- na.omit(t.nono[,cbind("newPTS_reversed","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_foodpGDP","Lag_lognonnegUS_fdi_chemicalpGDP","Lag_lognonnegUS_fdi_prim_fab_metalpGDP","Lag_lognonnegUS_fdi_machinerypGDP","Lag_lognonnegUS_fdi_electricalpGDP","Lag_lognonnegUS_fdi_transportpGDP","Lag_lognonnegUS_fdi_whole_tradepGDP","Lag_lognonnegUS_fdi_depositorypGDP", "Lag_newPTS_reversed" ,"country","year")])

# models for newPTS_reversed 
PTS.logit.LDV <- lrm(newPTS_reversed ~ Lag_lognonnegUS_fdi_totalpGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_newPTS_reversed,data=data.PTS.LDV,x=TRUE, y=TRUE)
PTS.logit.LDV.corr <- robcov(PTS.logit.LDV,data.PTS.LDV$country) #robust errors  

PTS.logit.LDV.UN <- lrm(newPTS_reversed ~ Lag_log_UN_FDI_stock_pgdp + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_newPTS_reversed,data=data.PTS1.LDV,x=TRUE, y=TRUE)
PTS.logit.LDV.UN.corr <- robcov(PTS.logit.LDV.UN,data.PTS1.LDV$country) #robust errors  

PTS.logit.LDV.excl <- lrm(newPTS_reversed ~ Lag_lognonnegUS_fdi_foodpGDP + Lag_lognonnegUS_fdi_chemicalpGDP + Lag_lognonnegUS_fdi_prim_fab_metalpGDP + Lag_lognonnegUS_fdi_machinerypGDP + Lag_lognonnegUS_fdi_electricalpGDP + Lag_lognonnegUS_fdi_transportpGDP + Lag_lognonnegUS_fdi_whole_tradepGDP + Lag_lognonnegUS_fdi_depositorypGDP           + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl  + Lag_newPTS_reversed,data=data.PTS.excl.LDV,x=TRUE, y=TRUE)
PTS.logit.LDV.excl.corr <- robcov(PTS.logit.LDV.excl,data.PTS.excl.LDV$country) 

names(PTS.logit.LDV.corr$coefficients)
names(PTS.logit.LDV.corr$coefficients)[names(PTS.logit.LDV.corr$coefficients) == "Lag_newPTS_reversed"] <- "Past"
rownames(PTS.logit.LDV.corr$var)[rownames(PTS.logit.LDV.corr$var)== "Lag_newPTS_reversed"] <- "Past"
colnames(PTS.logit.LDV.corr$var)[colnames(PTS.logit.LDV.corr$var)== "Lag_newPTS_reversed"] <- "Past"

names(PTS.logit.LDV.UN.corr$coefficients)
names(PTS.logit.LDV.UN.corr$coefficients)[names(PTS.logit.LDV.UN.corr$coefficients) == "Lag_newPTS_reversed"] <- "Past"
rownames(PTS.logit.LDV.UN.corr$var)[rownames(PTS.logit.LDV.UN.corr$var)== "Lag_newPTS_reversed"] <- "Past"
colnames(PTS.logit.LDV.UN.corr$var)[colnames(PTS.logit.LDV.UN.corr$var)== "Lag_newPTS_reversed"] <- "Past"

names(PTS.logit.LDV.excl.corr$coefficients)
names(PTS.logit.LDV.excl.corr$coefficients)[names(PTS.logit.LDV.excl.corr$coefficients) == "Lag_newPTS_reversed"] <- "Past"
rownames(PTS.logit.LDV.excl.corr$var)[rownames(PTS.logit.LDV.excl.corr$var)== "Lag_newPTS_reversed"] <- "Past"
colnames(PTS.logit.LDV.excl.corr$var)[colnames(PTS.logit.LDV.excl.corr$var)== "Lag_newPTS_reversed"] <- "Past"

# store results under new name:
PTS.logit.LDV.corr.newpts <- PTS.logit.LDV.corr
PTS.logit.LDV.UN.corr.newpts <-  PTS.logit.LDV.UN.corr
PTS.logit.LDV.excl.corr.newpts <-  PTS.logit.LDV.excl.corr












#?use PTS State Dept version ####
data.PTS.LDV <- na.omit(t.nono[,cbind("PTS_DoS_reversed","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_totalpGDP", "Lag_PTS_DoS_reversed", "country","year")])
data.PTS1.LDV <- na.omit(t.nono[,cbind("PTS_DoS_reversed","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_log_UN_FDI_stock_pgdp","Lag_PTS_DoS_reversed", "country","year")])
data.PTS.excl.LDV  <- na.omit(t.nono[,cbind("PTS_DoS_reversed","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_foodpGDP","Lag_lognonnegUS_fdi_chemicalpGDP","Lag_lognonnegUS_fdi_prim_fab_metalpGDP","Lag_lognonnegUS_fdi_machinerypGDP","Lag_lognonnegUS_fdi_electricalpGDP","Lag_lognonnegUS_fdi_transportpGDP","Lag_lognonnegUS_fdi_whole_tradepGDP","Lag_lognonnegUS_fdi_depositorypGDP","Lag_PTS_DoS_reversed","country","year")])

# models
PTS.logit.LDV <- lrm(PTS_DoS_reversed ~ Lag_lognonnegUS_fdi_totalpGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_PTS_DoS_reversed,data=data.PTS.LDV,x=TRUE, y=TRUE)
PTS.logit.LDV.corr.DoS <- robcov(PTS.logit.LDV,data.PTS.LDV$country) #robust errors clustered by country 


PTS.logit.LDV.UN <- lrm(PTS_DoS_reversed ~ Lag_log_UN_FDI_stock_pgdp + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_PTS_DoS_reversed,data=data.PTS1.LDV,x=TRUE, y=TRUE)
PTS.logit.LDV.UN.corr.DoS <- robcov(PTS.logit.LDV.UN,data.PTS1.LDV$country) #robust errors clustered by country 

PTS.logit.LDV.excl <- lrm(PTS_DoS_reversed ~ Lag_lognonnegUS_fdi_foodpGDP + Lag_lognonnegUS_fdi_chemicalpGDP + Lag_lognonnegUS_fdi_prim_fab_metalpGDP + Lag_lognonnegUS_fdi_machinerypGDP + Lag_lognonnegUS_fdi_electricalpGDP + Lag_lognonnegUS_fdi_transportpGDP + Lag_lognonnegUS_fdi_whole_tradepGDP + Lag_lognonnegUS_fdi_depositorypGDP           + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_PTS_DoS_reversed,data=data.PTS.excl.LDV,x=TRUE, y=TRUE)
PTS.logit.LDV.excl.corr <- robcov(PTS.logit.LDV.excl,data.PTS.excl.LDV$country) 


names(PTS.logit.LDV.corr.DoS$coefficients)
names(PTS.logit.LDV.corr.DoS$coefficients)[names(PTS.logit.LDV.corr.DoS$coefficients) == "Lag_PTS_DoS_reversed"] <- "Past"
rownames(PTS.logit.LDV.corr.DoS$var)[rownames(PTS.logit.LDV.corr.DoS$var)== "Lag_PTS_DoS_reversed"] <- "Past"
colnames(PTS.logit.LDV.corr.DoS$var)[colnames(PTS.logit.LDV.corr.DoS$var)== "Lag_PTS_DoS_reversed"] <- "Past"

names(PTS.logit.LDV.UN.corr.DoS$coefficients)
names(PTS.logit.LDV.UN.corr.DoS$coefficients)[names(PTS.logit.LDV.UN.corr.DoS$coefficients) == "Lag_PTS_DoS_reversed"] <- "Past"
rownames(PTS.logit.LDV.UN.corr.DoS$var)[rownames(PTS.logit.LDV.UN.corr.DoS$var)== "Lag_PTS_DoS_reversed"] <- "Past"
colnames(PTS.logit.LDV.UN.corr.DoS$var)[colnames(PTS.logit.LDV.UN.corr.DoS$var)== "Lag_PTS_DoS_reversed"] <- "Past"

names(PTS.logit.LDV.excl.corr$coefficients)
names(PTS.logit.LDV.excl.corr$coefficients)[names(PTS.logit.LDV.excl.corr$coefficients) == "Lag_PTS_DoS_reversed"] <- "Past"
rownames(PTS.logit.LDV.excl.corr$var)[rownames(PTS.logit.LDV.excl.corr$var)== "Lag_PTS_DoS_reversed"] <- "Past"
colnames(PTS.logit.LDV.excl.corr$var)[colnames(PTS.logit.LDV.excl.corr$var)== "Lag_PTS_DoS_reversed"] <- "Past"




#?large table with results on both PTS alternatives####
varnames <- c("Global FDI","U.S. total FDI","Food FDI","Chemical FDI","Metal FDI","Machinery FDI","Electrical FDI","Transport FDI", "Whole. Trade FDI","Depository FDI", "Trade", "GDP p. capita", "Population", "Democracy", "Conflict", "Past")
dvnames <- c("Overall","Overall","Overall","Dept of State","Dept of State","Dept of State")

stargazer(PTS.logit.LDV.UN.corr.newpts,PTS.logit.LDV.corr.newpts,PTS.logit.LDV.excl.corr.newpts,PTS.logit.LDV.UN.corr,PTS.logit.LDV.corr,PTS.logit.LDV.excl.corr, 
          covariate.labels=varnames,
       dep.var.labels.include = FALSE,
        column.labels=c("PTS Overall","PTS State Dept"),
       column.separate=c(3,3),
omit=c("y>=1","y>=2","y>=3","y>=4","y>=5","y>=6","y>=7","y>=8","factor","Constant","(Intercept)"), omit.stat = c("f","ser","chi2"),nobs=T,title="Using Alternative Versions of the Political Terror Scale",no.space=T,font.size="scriptsize", notes=c("Ordered logit with country-clustered standard errors. OLS with panel corrected standard errors.","All FDI is divided by GDP, then logged. GDPpc, population, trade are logged.","Past is the lagged dependent variable (LDV).","PTS overall is calculated as PTS Amnesty + PTS State Dept., divided by 2."),notes.align="l",type="text",label="alternativePTS.tex",out="alternativePTS.tex")




















# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Creating table "Total FDI and Personal Integrity Rights Protection (including time variable)" ####
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

              # create time variable 
              class(t.nono$year)
              t.nono$time <- t.nono$year - min(t.nono$year)
              
              # create decade dummy (I ended up not using decade dummies)
              t.nono$decade <- ifelse(t.nono$year < 1990, 1980, 
              ifelse(t.nono$year > 1999, 2000, 1990))
              summary(t.nono$decade)
              
              # data sets####
              
              data.CIRI.PHY.LDV <- na.omit(t.nono[,cbind("CIRI_PHYSINT","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_totalpGDP","Lag_CIRI_PHYSINT","time","decade", "country","year")])
              
              
              data.PTS.LDV <- na.omit(t.nono[,cbind("PTS_ai_reversed","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_totalpGDP", "Lag_PTS_ai_reversed","time","decade",  "country","year")])
              
              
              data.Fariss.LDV <- na.omit(t.nono[,cbind("Fariss_HR","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_totalpGDP", "Lag_Fariss_HR","time","decade", "country","year")]) 
              data.Fariss.LDV$country <- factor(as.character(data.Fariss.LDV$country)) #from 192 to 118
              
              
              
              # For total UN FDI:
              
              data.CIRI.PHY1.LDV <- na.omit(t.nono[,cbind("CIRI_PHYSINT","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_log_UN_FDI_stock_pgdp" ,"Lag_CIRI_PHYSINT","time","decade", "country","year")])
              
              
              data.PTS1.LDV <- na.omit(t.nono[,cbind("PTS_ai_reversed","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_log_UN_FDI_stock_pgdp","Lag_PTS_ai_reversed", "time","decade", "country","year")])
              
              
              data.Fariss1.LDV <- na.omit(t.nono[,cbind("Fariss_HR","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_log_UN_FDI_stock_pgdp", "Lag_Fariss_HR","time","decade", "country","year")])
              
              # data for SECTORAL MODELS###########
              
              data.CIRI.PHY.excl.LDV <- na.omit(t.nono[,cbind("CIRI_PHYSINT","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_foodpGDP","Lag_lognonnegUS_fdi_chemicalpGDP","Lag_lognonnegUS_fdi_prim_fab_metalpGDP","Lag_lognonnegUS_fdi_machinerypGDP","Lag_lognonnegUS_fdi_electricalpGDP","Lag_lognonnegUS_fdi_transportpGDP","Lag_lognonnegUS_fdi_whole_tradepGDP","Lag_lognonnegUS_fdi_depositorypGDP","Lag_CIRI_PHYSINT","time","decade", "country","year")])
              data.PTS.excl.LDV  <- na.omit(t.nono[,cbind("PTS_ai_reversed","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_foodpGDP","Lag_lognonnegUS_fdi_chemicalpGDP","Lag_lognonnegUS_fdi_prim_fab_metalpGDP","Lag_lognonnegUS_fdi_machinerypGDP","Lag_lognonnegUS_fdi_electricalpGDP","Lag_lognonnegUS_fdi_transportpGDP","Lag_lognonnegUS_fdi_whole_tradepGDP","Lag_lognonnegUS_fdi_depositorypGDP","Lag_PTS_ai_reversed","time","decade","country","year")])
              data.Fariss.excl.LDV <- na.omit(t.nono[,cbind("Fariss_HR","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_foodpGDP","Lag_lognonnegUS_fdi_chemicalpGDP","Lag_lognonnegUS_fdi_prim_fab_metalpGDP","Lag_lognonnegUS_fdi_machinerypGDP","Lag_lognonnegUS_fdi_electricalpGDP","Lag_lognonnegUS_fdi_transportpGDP","Lag_lognonnegUS_fdi_whole_tradepGDP","Lag_lognonnegUS_fdi_depositorypGDP","Lag_Fariss_HR","time","decade","country","year")])
              
              
              # Models including time####
              CIRI.logit.LDV <- lrm(CIRI_PHYSINT ~ Lag_lognonnegUS_fdi_totalpGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + time +  Lag_CIRI_PHYSINT,data=data.CIRI.PHY.LDV,x=TRUE, y=TRUE)
              CIRI.logit.LDV.corr <- robcov(CIRI.logit.LDV,data.CIRI.PHY.LDV$country) #robust errors clustered by country 
              
              PTS.logit.LDV <- lrm(PTS_ai_reversed ~ Lag_lognonnegUS_fdi_totalpGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + time + Lag_PTS_ai_reversed,data=data.PTS.LDV,x=TRUE, y=TRUE)
              PTS.logit.LDV.corr <- robcov(PTS.logit.LDV,data.PTS.LDV$country) #robust errors clustered by country 
              
              #Fariss xtpcse with LDV
              data.Fariss.LDV <- as.data.frame(data.Fariss.LDV)
              form   <- Fariss_HR ~ Lag_lognonnegUS_fdi_totalpGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + time + Lag_Fariss_HR
              res.po <- plm(form,model="pooling",index=c("country","year"),data=data.Fariss.LDV)
              summary(res.po)      
              ## PCSE a la Beck-Katz
              res.BKtime  <- vcovBK(x=res.po,cluster="time")
              ct.time     <- coeftest(res.po,vcov=res.BKtime)
              print(ct.time)
             
              
              # rename lagged values to get them into one row with stargazer; replace plm se's with corrected ones
              names(CIRI.logit.LDV.corr$coefficients)
              names(CIRI.logit.LDV.corr$coefficients)[names(CIRI.logit.LDV.corr$coefficients) == "Lag_CIRI_PHYSINT"] <- "Past"
              rownames(CIRI.logit.LDV.corr$var)[rownames(CIRI.logit.LDV.corr$var)== "Lag_CIRI_PHYSINT"] <- "Past"
              colnames(CIRI.logit.LDV.corr$var)[colnames(CIRI.logit.LDV.corr$var)== "Lag_CIRI_PHYSINT"] <- "Past"
              
              names(PTS.logit.LDV.corr$coefficients)
              names(PTS.logit.LDV.corr$coefficients)[names(PTS.logit.LDV.corr$coefficients) == "Lag_PTS_ai_reversed"] <- "Past"
              rownames(PTS.logit.LDV.corr$var)[rownames(PTS.logit.LDV.corr$var)== "Lag_PTS_ai_reversed"] <- "Past"
              colnames(PTS.logit.LDV.corr$var)[colnames(PTS.logit.LDV.corr$var)== "Lag_PTS_ai_reversed"] <- "Past"
              
              res.po$vcov <- res.BKtime  # replacing the first uncorrected residual covariance structure with the corrected one see str()
              names(res.po$coefficients)[names(res.po$coefficients) == "Lag_Fariss_HR"] <- "Past"
              rownames(res.po$vcov)[rownames(res.po$vcov)== "Lag_Fariss_HR"] <- "Past"
              colnames(res.po$vcov)[colnames(res.po$vcov)== "Lag_Fariss_HR"] <- "Past"
              
              #UN
              CIRI.logit.LDV.UN <- lrm(CIRI_PHYSINT ~ Lag_log_UN_FDI_stock_pgdp + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + time +  Lag_CIRI_PHYSINT,data=data.CIRI.PHY1.LDV ,x=TRUE, y=TRUE)
              CIRI.logit.LDV.UN.corr <- robcov(CIRI.logit.LDV.UN,data.CIRI.PHY1.LDV$country) #robust errors clustered by country 
              
              PTS.logit.LDV.UN <- lrm(PTS_ai_reversed ~ Lag_log_UN_FDI_stock_pgdp + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + time +  Lag_PTS_ai_reversed,data=data.PTS1.LDV,x=TRUE, y=TRUE)
              PTS.logit.LDV.UN.corr <- robcov(PTS.logit.LDV.UN,data.PTS1.LDV$country) #robust errors clustered by country 
              
              # Total FDI Fariss 
              data.Fariss.1.LDV <- as.data.frame(data.Fariss1.LDV)
              form   <- Fariss_HR ~ Lag_log_UN_FDI_stock_pgdp + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + time + Lag_Fariss_HR
              res.po1 <- plm(form,model="pooling",index=c("country","year"),data=data.Fariss1.LDV)
              summary(res.po1)      
              ## PCSE a la Beck-Katz
              res.BKtime1  <- vcovBK(x=res.po1,cluster="time")
              ct.time1     <- coeftest(res.po1,vcov=res.BKtime1)
              print(ct.time1) 
              
              # rename lagged values to get them into one row
              names(CIRI.logit.LDV.UN.corr$coefficients)
              names(CIRI.logit.LDV.UN.corr$coefficients)[names(CIRI.logit.LDV.UN.corr$coefficients) == "Lag_CIRI_PHYSINT"] <- "Past"
              rownames(CIRI.logit.LDV.UN.corr$var)[rownames(CIRI.logit.LDV.UN.corr$var)== "Lag_CIRI_PHYSINT"] <- "Past"
              colnames(CIRI.logit.LDV.UN.corr$var)[colnames(CIRI.logit.LDV.UN.corr$var)== "Lag_CIRI_PHYSINT"] <- "Past"
              names(PTS.logit.LDV.UN.corr$coefficients)
              names(PTS.logit.LDV.UN.corr$coefficients)[names(PTS.logit.LDV.UN.corr$coefficients) == "Lag_PTS_ai_reversed"] <- "Past"
              rownames(PTS.logit.LDV.UN.corr$var)[rownames(PTS.logit.LDV.UN.corr$var)== "Lag_PTS_ai_reversed"] <- "Past"
              colnames(PTS.logit.LDV.UN.corr$var)[colnames(PTS.logit.LDV.UN.corr$var)== "Lag_PTS_ai_reversed"] <- "Past"
              
              res.po1$vcov <- res.BKtime1  # replacing the first uncorrected residual covariance structure with the corrected one see str()
              names(res.po1$coefficients)[names(res.po1$coefficients) == "Lag_Fariss_HR"] <- "Past"
              rownames(res.po1$vcov)[rownames(res.po1$vcov)== "Lag_Fariss_HR"] <- "Past"
              colnames(res.po1$vcov)[colnames(res.po1$vcov)== "Lag_Fariss_HR"] <- "Past"
              
              # Large Table for both Total FDI (US, UN) ####
              varnames <- c("Global FDI p.GDP","U.S. FDI p. GDP", "Trade", "GDP p. capita", "Population", "Democracy", "Conflict","Time","Past")
              dvnames <- c("CIRI Integrity","PTS Amnesty","Latent Scores","CIRI Integrity","PTS Amnesty","Latent Scores")
              stargazer(CIRI.logit.LDV.UN.corr, PTS.logit.LDV.UN.corr,res.po1,CIRI.logit.LDV.corr,PTS.logit.LDV.corr,res.po,
              dep.var.labels=dvnames,
              covariate.labels=varnames,
              omit=c("y>=1","y>=2","y>=3","y>=4","y>=5","y>=6","y>=7","y>=8","factor","Constant","(Intercept)"),
              type="text",omit.stat=c("chi2","f","ser"),notes=c("Including a year continuous time variable to account for temporal variation in measures over time.","Ordered logit with country-clustered standard errors. OLS with panel corrected standard errors.","FDIpGDP, GDPpc, population, trade are logged. Past is the lagged dependent variable (LDV).","Years included: 1983-2010."),font.size="scriptsize",notes.align="l",title="Total FDI and Personal Integrity Rights, including time variable",label="total.time.tex",out="total.time.tex")
              



              
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Creating table "Sectoral FDI and Personal Integrity Rights (including time variable)" ####
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

              CIRI.logit.LDV.excl <- lrm(CIRI_PHYSINT ~ Lag_lognonnegUS_fdi_foodpGDP + Lag_lognonnegUS_fdi_chemicalpGDP + Lag_lognonnegUS_fdi_prim_fab_metalpGDP + Lag_lognonnegUS_fdi_machinerypGDP + Lag_lognonnegUS_fdi_electricalpGDP + Lag_lognonnegUS_fdi_transportpGDP + Lag_lognonnegUS_fdi_whole_tradepGDP + Lag_lognonnegUS_fdi_depositorypGDP+ Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + time + Lag_CIRI_PHYSINT,data=data.CIRI.PHY.excl.LDV  ,x=TRUE, y=TRUE)
              CIRI.logit.LDV.excl.corr <- robcov(CIRI.logit.LDV.excl,data.CIRI.PHY.excl.LDV$country) #robust errors clustered by country 
              
              PTS.logit.LDV.excl <- lrm(PTS_ai_reversed ~ Lag_lognonnegUS_fdi_foodpGDP + Lag_lognonnegUS_fdi_chemicalpGDP + Lag_lognonnegUS_fdi_prim_fab_metalpGDP + Lag_lognonnegUS_fdi_machinerypGDP + Lag_lognonnegUS_fdi_electricalpGDP + Lag_lognonnegUS_fdi_transportpGDP + Lag_lognonnegUS_fdi_whole_tradepGDP + Lag_lognonnegUS_fdi_depositorypGDP+ Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl +  time +Lag_PTS_ai_reversed,data=data.PTS.excl.LDV,x=TRUE, y=TRUE)
              PTS.logit.LDV.excl.corr <- robcov(PTS.logit.LDV.excl,data.PTS.excl.LDV$country) 
              
              data.Fariss.excl.LDV <- as.data.frame(data.Fariss.excl.LDV)
              form   <- Fariss_HR ~  Lag_lognonnegUS_fdi_foodpGDP + Lag_lognonnegUS_fdi_chemicalpGDP + Lag_lognonnegUS_fdi_prim_fab_metalpGDP + Lag_lognonnegUS_fdi_machinerypGDP + Lag_lognonnegUS_fdi_electricalpGDP + Lag_lognonnegUS_fdi_transportpGDP + Lag_lognonnegUS_fdi_whole_tradepGDP + Lag_lognonnegUS_fdi_depositorypGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl +  time +Lag_Fariss_HR
              res.po.excl <- plm(form,model="pooling",index=c("country","year"),data=data.Fariss.excl.LDV)
              summary(res.po.excl)      
              ## PCSE a la Beck-Katz
              res.BKtime.excl  <- vcovBK(x=res.po.excl,cluster="time")
              ct.time.excl     <- coeftest(res.po.excl,vcov=res.BKtime.excl)
              print(ct.time.excl)
              
              # rename lagged values to get them into one row
              names(CIRI.logit.LDV.excl.corr$coefficients)
              names(CIRI.logit.LDV.excl.corr$coefficients)[names(CIRI.logit.LDV.excl.corr$coefficients) == "Lag_CIRI_PHYSINT"] <- "Past"
              rownames(CIRI.logit.LDV.excl.corr$var)[rownames(CIRI.logit.LDV.excl.corr$var)== "Lag_CIRI_PHYSINT"] <- "Past"
              colnames(CIRI.logit.LDV.excl.corr$var)[colnames(CIRI.logit.LDV.excl.corr$var)== "Lag_CIRI_PHYSINT"] <- "Past"
              names(PTS.logit.LDV.excl.corr$coefficients)
              names(PTS.logit.LDV.excl.corr$coefficients)[names(PTS.logit.LDV.excl.corr$coefficients) == "Lag_PTS_ai_reversed"] <- "Past"
              rownames(PTS.logit.LDV.excl.corr$var)[rownames(PTS.logit.LDV.excl.corr$var)== "Lag_PTS_ai_reversed"] <- "Past"
              colnames(PTS.logit.LDV.excl.corr$var)[colnames(PTS.logit.LDV.excl.corr$var)== "Lag_PTS_ai_reversed"] <- "Past"
              
              
              res.po.excl$vcov <- res.BKtime.excl  # replacing the first uncorrected residual covariance structure with the corrected one see str()
              names(res.po.excl$coefficients)[names(res.po.excl$coefficients) == "Lag_Fariss_HR"] <- "Past"
              rownames(res.po.excl$vcov)[rownames(res.po.excl$vcov)== "Lag_Fariss_HR"] <- "Past"
              colnames(res.po.excl$vcov)[colnames(res.po.excl$vcov)== "Lag_Fariss_HR"] <- "Past"
              
              
              # table
              varnames <- c("Food FDI","Chemical FDI","Metal FDI","Machinery FDI","Electrical FDI","Transport FDI", "Wholesale FDI","Depository FDI", "Trade", "GDP p. capita", "Population", "Democracy", "Conflict","Time", "Past")
              dvnames <- c("CIRI Integrity","PTS Amnesty","Latent Scores","CIRI Integrity","PTS Amnesty","Latent Scores")
              stargazer(CIRI.logit.LDV.excl.corr,PTS.logit.LDV.excl.corr,res.po.excl,
              covariate.labels=varnames, dep.var.labels=dvnames, 
              omit=c("y>=1","y>=2","y>=3","y>=4","y>=5","y>=6","y>=7","y>=8","factor","Constant","(Intercept)"), 
              omit.stat = c("f","ser","chi2"),
              nobs=T,title="Sectoral FDI and Personal Integrity Rights, including time variable",no.space=T,font.size="scriptsize", notes=c("Robustness: Using a year continuous time variable","to account for temporal variation in measures over time.","Ordered logit with country-clustered standard errors.","OLS with panel corrected standard errors.","FDIpGDP, GDPpc, population, trade are logged.","Past is the lagged dependent variable (LDV)."), add.lines = list(c("Years", "1983-1999", "1983-1999","1983-1999","2000-2010","2000-2010","2000-2010")),notes.align="l",type="text",label="Sectoral.time.tex",out="Sectoral.time.tex")
























 
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Creating table "Total FDI and Personal Integrity Rights Protection (1983-2010), including region dummies"####
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

# EAP: East Asia & Pacific according to World Bank Classification
              # SAS: South Asia according to World Bank Classification
              # ECA: Europe & Central Asia according to World Bank Classification -- this will be my reference group in dummies, see http://www.theanalysisfactor.com/strategies-dummy-coding/
              # LAC: Latin America & Caribbean according to World Bank Classification
              # MNA: Middle East & North Africa according to World Bank Classification
              # SSA: Sub-Saharan Africa according to World Bank Classification
              # North America (is removed from most of hte data sets below after listwise deletion)
              
              # data including wb_code to create dummmies later
              data.CIRI.PHY.LDV <- na.omit(t.nono[,cbind("CIRI_PHYSINT","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_totalpGDP","Lag_CIRI_PHYSINT", "country","year","wb_region")])
              data.PTS.LDV <- na.omit(t.nono[,cbind("PTS_ai_reversed","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_totalpGDP", "Lag_PTS_ai_reversed", "country","year","wb_region")])
              data.Fariss.LDV <- na.omit(t.nono[,cbind("Fariss_HR","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_totalpGDP", "Lag_Fariss_HR","country","year","wb_region")]) 
              data.Fariss.LDV$country <- factor(as.character(data.Fariss.LDV$country)) #
              
              
              # change referenc category for dummies
              str(data.CIRI.PHY.LDV) #show levels (first one is usually taken as ref category, which is North America)
              table(data.CIRI.PHY.LDV$wb_region) #which is the largest sample size? SSA
              data.CIRI.PHY.LDV <- within(data.CIRI.PHY.LDV, wb_region <- relevel(wb_region, ref = "ECA"))
              str(data.CIRI.PHY.LDV) #show levels (first one is usually taken as ref category, which is now SSA)
              data.PTS.LDV <- within(data.PTS.LDV, wb_region <- relevel(wb_region, ref = "ECA"))
              data.Fariss.LDV <- within(data.Fariss.LDV, wb_region <- relevel(wb_region, ref = "ECA"))
              
              
              # For total UN FDI:
              data.CIRI.PHY1.LDV <- na.omit(t.nono[,cbind("CIRI_PHYSINT","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_log_UN_FDI_stock_pgdp" ,"Lag_CIRI_PHYSINT","country","year","wb_region")])
              data.PTS1.LDV <- na.omit(t.nono[,cbind("PTS_ai_reversed","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_log_UN_FDI_stock_pgdp","Lag_PTS_ai_reversed", "country","year","wb_region")])
              data.Fariss1.LDV <- na.omit(t.nono[,cbind("Fariss_HR","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_log_UN_FDI_stock_pgdp", "Lag_Fariss_HR","country","year","wb_region")])
              
              data.CIRI.PHY1.LDV <- within(data.CIRI.PHY1.LDV, wb_region <- relevel(wb_region, ref = "ECA"))
              data.PTS1.LDV <- within(data.PTS1.LDV, wb_region <- relevel(wb_region, ref = "ECA"))
              data.Fariss1.LDV <- within(data.Fariss1.LDV, wb_region <- relevel(wb_region, ref = "ECA"))
              
              
              # Data for sectoral models
              data.CIRI.PHY.excl.LDV <- na.omit(t.nono[,cbind("CIRI_PHYSINT","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_foodpGDP","Lag_lognonnegUS_fdi_chemicalpGDP","Lag_lognonnegUS_fdi_prim_fab_metalpGDP","Lag_lognonnegUS_fdi_machinerypGDP","Lag_lognonnegUS_fdi_electricalpGDP","Lag_lognonnegUS_fdi_transportpGDP","Lag_lognonnegUS_fdi_whole_tradepGDP","Lag_lognonnegUS_fdi_depositorypGDP","Lag_CIRI_PHYSINT", "country","year","wb_region")])
              data.PTS.excl.LDV  <- na.omit(t.nono[,cbind("PTS_ai_reversed","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_foodpGDP","Lag_lognonnegUS_fdi_chemicalpGDP","Lag_lognonnegUS_fdi_prim_fab_metalpGDP","Lag_lognonnegUS_fdi_machinerypGDP","Lag_lognonnegUS_fdi_electricalpGDP","Lag_lognonnegUS_fdi_transportpGDP","Lag_lognonnegUS_fdi_whole_tradepGDP","Lag_lognonnegUS_fdi_depositorypGDP","Lag_PTS_ai_reversed","country","year","wb_region")])
              data.Fariss.excl.LDV <- na.omit(t.nono[,cbind("Fariss_HR","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_foodpGDP","Lag_lognonnegUS_fdi_chemicalpGDP","Lag_lognonnegUS_fdi_prim_fab_metalpGDP","Lag_lognonnegUS_fdi_machinerypGDP","Lag_lognonnegUS_fdi_electricalpGDP","Lag_lognonnegUS_fdi_transportpGDP","Lag_lognonnegUS_fdi_whole_tradepGDP","Lag_lognonnegUS_fdi_depositorypGDP","Lag_Fariss_HR","country","year","wb_region")])
              
              data.CIRI.PHY.excl.LDV <- within(data.CIRI.PHY.excl.LDV, wb_region <- relevel(wb_region, ref = "ECA"))
              data.PTS.excl.LDV <- within(data.PTS.excl.LDV, wb_region <- relevel(wb_region, ref = "ECA"))
              data.Fariss.excl.LDV <- within(data.Fariss.excl.LDV, wb_region <- relevel(wb_region, ref = "ECA"))
              
              
              # main region fe models including factor(wb_region)-1 ####
              
              # Main Part Models ####
              #US FDI
              CIRI.logit.LDV <- lrm(CIRI_PHYSINT ~ Lag_lognonnegUS_fdi_totalpGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_CIRI_PHYSINT+factor(wb_region)-1,data=data.CIRI.PHY.LDV,x=TRUE, y=TRUE)
              CIRI.logit.LDV.corr <- robcov(CIRI.logit.LDV,data.CIRI.PHY.LDV$country) #robust errors clustered by country  
              
              PTS.logit.LDV <- lrm(PTS_ai_reversed ~ Lag_lognonnegUS_fdi_totalpGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_PTS_ai_reversed+factor(wb_region)-1,data=data.PTS.LDV,x=TRUE, y=TRUE)
              PTS.logit.LDV.corr <- robcov(PTS.logit.LDV,data.PTS.LDV$country) #robust errors clustered by country 
              
              #Fariss xtpcse with LDV
              data.Fariss.LDV <- as.data.frame(data.Fariss.LDV)
              form   <- Fariss_HR ~ Lag_lognonnegUS_fdi_totalpGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_Fariss_HR +factor(wb_region)
              res.po <- plm(form,model="pooling",index=c("country","year"),data=data.Fariss.LDV)
              summary(res.po)      
              ## PCSE a la Beck-Katz
              res.BKtime  <- vcovBK(x=res.po,cluster="time")
              ct.time     <- coeftest(res.po,vcov=res.BKtime)
              print(ct.time)
              
              # rename lagged values to get them into one row with stargazer; replace plm se's with corrected ones
              names(CIRI.logit.LDV.corr$coefficients)
              names(CIRI.logit.LDV.corr$coefficients)[names(CIRI.logit.LDV.corr$coefficients) == "Lag_CIRI_PHYSINT"] <- "Past"
              rownames(CIRI.logit.LDV.corr$var)[rownames(CIRI.logit.LDV.corr$var)== "Lag_CIRI_PHYSINT"] <- "Past"
              colnames(CIRI.logit.LDV.corr$var)[colnames(CIRI.logit.LDV.corr$var)== "Lag_CIRI_PHYSINT"] <- "Past"
              
              names(PTS.logit.LDV.corr$coefficients)
              names(PTS.logit.LDV.corr$coefficients)[names(PTS.logit.LDV.corr$coefficients) == "Lag_PTS_ai_reversed"] <- "Past"
              rownames(PTS.logit.LDV.corr$var)[rownames(PTS.logit.LDV.corr$var)== "Lag_PTS_ai_reversed"] <- "Past"
              colnames(PTS.logit.LDV.corr$var)[colnames(PTS.logit.LDV.corr$var)== "Lag_PTS_ai_reversed"] <- "Past"
              
              res.po$vcov <- res.BKtime  # replacing the first uncorrected residual covariance structure with the corrected one see str()
              names(res.po$coefficients)[names(res.po$coefficients) == "Lag_Fariss_HR"] <- "Past"
              rownames(res.po$vcov)[rownames(res.po$vcov)== "Lag_Fariss_HR"] <- "Past"
              colnames(res.po$vcov)[colnames(res.po$vcov)== "Lag_Fariss_HR"] <- "Past"
              
              names(res.po$coefficients)[names(res.po$coefficients) == "factor(wb_region)ECA"] <- "wb_region=ECA"
              names(res.po$coefficients)[names(res.po$coefficients) == "factor(wb_region)LAC"] <- "wb_region=LAC"
              names(res.po$coefficients)[names(res.po$coefficients) == "factor(wb_region)MNA"] <- "wb_region=MNA"
              names(res.po$coefficients)[names(res.po$coefficients) == "factor(wb_region)SAS"] <- "wb_region=SAS"
              names(res.po$coefficients)[names(res.po$coefficients) == "factor(wb_region)SSA"] <- "wb_region=SSA"
              names(res.po$coefficients)[names(res.po$coefficients) == "factor(wb_region)EAP"] <- "wb_region=EAP"
              rownames(res.po$vcov)[rownames(res.po$vcov)== "factor(wb_region)ECA"] <- "wb_region=ECA"
              colnames(res.po$vcov)[colnames(res.po$vcov)== "factor(wb_region)ECA"] <- "wb_region=ECA"
              rownames(res.po$vcov)[rownames(res.po$vcov)== "factor(wb_region)LAC"] <- "wb_region=LAC"
              colnames(res.po$vcov)[colnames(res.po$vcov)== "factor(wb_region)LAC"] <- "wb_region=LAC"
              rownames(res.po$vcov)[rownames(res.po$vcov)== "factor(wb_region)MNA"] <- "wb_region=MNA"
              colnames(res.po$vcov)[colnames(res.po$vcov)== "factor(wb_region)MNA"] <- "wb_region=MNA"
              rownames(res.po$vcov)[rownames(res.po$vcov)== "factor(wb_region)SAS"] <- "wb_region=SAS"
              colnames(res.po$vcov)[colnames(res.po$vcov)== "factor(wb_region)SAS"] <- "wb_region=SAS"
              rownames(res.po$vcov)[rownames(res.po$vcov)== "factor(wb_region)SSA"] <- "wb_region=SSA"
              colnames(res.po$vcov)[colnames(res.po$vcov)== "factor(wb_region)SSA"] <- "wb_region=SSA"
              rownames(res.po$vcov)[rownames(res.po$vcov)== "factor(wb_region)EAP"] <- "wb_region=EAP"
              colnames(res.po$vcov)[colnames(res.po$vcov)== "factor(wb_region)EAP"] <- "wb_region=EAP"
              
              # Results Total UN FDI 
              CIRI.logit.LDV.UN <- lrm(CIRI_PHYSINT ~ Lag_log_UN_FDI_stock_pgdp + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_CIRI_PHYSINT+factor(wb_region)-1 ,data=data.CIRI.PHY1.LDV ,x=TRUE, y=TRUE)
              CIRI.logit.LDV.UN.corr <- robcov(CIRI.logit.LDV.UN,data.CIRI.PHY1.LDV$country) #robust errors clustered by country
              
              PTS.logit.LDV.UN <- lrm(PTS_ai_reversed ~ Lag_log_UN_FDI_stock_pgdp + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_PTS_ai_reversed+factor(wb_region)-1 ,data=data.PTS1.LDV,x=TRUE, y=TRUE)
              PTS.logit.LDV.UN.corr <- robcov(PTS.logit.LDV.UN,data.PTS1.LDV$country) #robust errors clustered by country 
              
              data.Fariss.1.LDV <- as.data.frame(data.Fariss1.LDV)
              form   <- Fariss_HR ~ Lag_log_UN_FDI_stock_pgdp + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_Fariss_HR+factor(wb_region)
              res.po1 <- plm(form,model="pooling",index=c("country","year"),data=data.Fariss1.LDV)
              summary(res.po1)      
              ## PCSE a la Beck-Katz
              res.BKtime1  <- vcovBK(x=res.po1,cluster="time")
              ct.time1     <- coeftest(res.po1,vcov=res.BKtime1)
              print(ct.time1) 
              
              # rename lagged values to get them into one row
              names(CIRI.logit.LDV.UN.corr$coefficients)
              names(CIRI.logit.LDV.UN.corr$coefficients)[names(CIRI.logit.LDV.UN.corr$coefficients) == "Lag_CIRI_PHYSINT"] <- "Past"
              rownames(CIRI.logit.LDV.UN.corr$var)[rownames(CIRI.logit.LDV.UN.corr$var)== "Lag_CIRI_PHYSINT"] <- "Past"
              colnames(CIRI.logit.LDV.UN.corr$var)[colnames(CIRI.logit.LDV.UN.corr$var)== "Lag_CIRI_PHYSINT"] <- "Past"
              names(PTS.logit.LDV.UN.corr$coefficients)
              names(PTS.logit.LDV.UN.corr$coefficients)[names(PTS.logit.LDV.UN.corr$coefficients) == "Lag_PTS_ai_reversed"] <- "Past"
              rownames(PTS.logit.LDV.UN.corr$var)[rownames(PTS.logit.LDV.UN.corr$var)== "Lag_PTS_ai_reversed"] <- "Past"
              colnames(PTS.logit.LDV.UN.corr$var)[colnames(PTS.logit.LDV.UN.corr$var)== "Lag_PTS_ai_reversed"] <- "Past"
              
              res.po1$vcov <- res.BKtime1  # replacing the first uncorrected residual covariance structure with the corrected one see str()
              names(res.po1$coefficients)[names(res.po1$coefficients) == "Lag_Fariss_HR"] <- "Past"
              rownames(res.po1$vcov)[rownames(res.po1$vcov)== "Lag_Fariss_HR"] <- "Past"
              colnames(res.po1$vcov)[colnames(res.po1$vcov)== "Lag_Fariss_HR"] <- "Past"
              
              #adjust region dummy names for Fariss to match all others
              names(res.po1$coefficients)[names(res.po1$coefficients) == "factor(wb_region)ECA"] <- "wb_region=ECA"
              names(res.po1$coefficients)[names(res.po1$coefficients) == "factor(wb_region)LAC"] <- "wb_region=LAC"
              names(res.po1$coefficients)[names(res.po1$coefficients) == "factor(wb_region)MNA"] <- "wb_region=MNA"
              names(res.po1$coefficients)[names(res.po1$coefficients) == "factor(wb_region)SAS"] <- "wb_region=SAS"
              names(res.po1$coefficients)[names(res.po1$coefficients) == "factor(wb_region)SSA"] <- "wb_region=SSA"
              names(res.po1$coefficients)[names(res.po1$coefficients) == "factor(wb_region)EAP"] <- "wb_region=EAP"
              rownames(res.po1$vcov)[rownames(res.po1$vcov)== "factor(wb_region)ECA"] <- "wb_region=ECA"
              colnames(res.po1$vcov)[colnames(res.po1$vcov)== "factor(wb_region)ECA"] <- "wb_region=ECA"
              rownames(res.po1$vcov)[rownames(res.po1$vcov)== "factor(wb_region)LAC"] <- "wb_region=LAC"
              colnames(res.po1$vcov)[colnames(res.po1$vcov)== "factor(wb_region)LAC"] <- "wb_region=LAC"
              rownames(res.po1$vcov)[rownames(res.po1$vcov)== "factor(wb_region)MNA"] <- "wb_region=MNA"
              colnames(res.po1$vcov)[colnames(res.po1$vcov)== "factor(wb_region)MNA"] <- "wb_region=MNA"
              rownames(res.po1$vcov)[rownames(res.po1$vcov)== "factor(wb_region)SAS"] <- "wb_region=SAS"
              colnames(res.po1$vcov)[colnames(res.po1$vcov)== "factor(wb_region)SAS"] <- "wb_region=SAS"
              rownames(res.po1$vcov)[rownames(res.po1$vcov)== "factor(wb_region)SSA"] <- "wb_region=SSA"
              colnames(res.po1$vcov)[colnames(res.po1$vcov)== "factor(wb_region)SSA"] <- "wb_region=SSA"
              rownames(res.po1$vcov)[rownames(res.po1$vcov)== "factor(wb_region)EAP"] <- "wb_region=EAP"
              colnames(res.po1$vcov)[colnames(res.po1$vcov)== "factor(wb_region)EAP"] <- "wb_region=EAP"
              
              
              varnames <- c("Global FDI p. GDP","US FDI p. GDP", "Trade", "GDP p. capita", "Population", "Democracy", "Conflict","Past", 
              "East Asia and Pacific",
              "Latin America and Caribbean",
              "Middle East and North Africa",
              "South Asia",
              "Sub-Saharan Africa")
              dvnames <- c("CIRI Integrity","PTS Amnesty","Latent Scores","CIRI Integrity","PTS Amnesty","Latent Scores")
              stargazer(CIRI.logit.LDV.UN.corr, PTS.logit.LDV.UN.corr,res.po1,CIRI.logit.LDV.corr,PTS.logit.LDV.corr,res.po,
              dep.var.labels=dvnames,
              covariate.labels=varnames,
              omit=c("y>=1","y>=2","y>=3","y>=4","y>=5","y>=6","y>=7","y>=8","factor","Constant","(Intercept)"),
              type="text",omit.stat=c("chi2","f","ser"),notes=c("Ordered logit with country-clustered standard errors in parentheses below the coefficient estimates.","OLS with panel corrected standard errors (PCSE) in parentheses below the coefficient estimates.","FDIpGDP, GDPpc, population, trade are logged. Past is the lagged dependent variable (LDV).","Region classification according to World Bank.","Europe and Central Asia is the reference category for region dummies and not displayed."),font.size="scriptsize",notes.align="l",title="Total FDI and Personal Integrity Rights, incl. region dummies",label="total.region.dummies.tex",out="total.region.dummies.tex")
              
              
              
              
# # # # # # # #  # # # #  # # # #  # # # #  # # # #  # # # #  # # # #  # # # #  # # # #  # # # #  # # # #  # # # #                
#Creating table "Sectoral U.S. FDI and Personal Integrity Rights Protection (1983-2010), including region dummies"####
# # # #  # # # #  # # # #  # # # #  # # # #  # # # #  # # # #  # # # #  # # # #  # # # #  # # # #  # # # #  # # # #  

              CIRI.logit.LDV.excl <- lrm(CIRI_PHYSINT ~ Lag_lognonnegUS_fdi_foodpGDP + Lag_lognonnegUS_fdi_chemicalpGDP + Lag_lognonnegUS_fdi_prim_fab_metalpGDP + Lag_lognonnegUS_fdi_machinerypGDP + Lag_lognonnegUS_fdi_electricalpGDP + Lag_lognonnegUS_fdi_transportpGDP + Lag_lognonnegUS_fdi_whole_tradepGDP + Lag_lognonnegUS_fdi_depositorypGDP+ Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_CIRI_PHYSINT +factor(wb_region),data=data.CIRI.PHY.excl.LDV  ,x=TRUE, y=TRUE)
              CIRI.logit.LDV.excl.corr <- robcov(CIRI.logit.LDV.excl,data.CIRI.PHY.excl.LDV$country) #robust errors clustered by country
              
              PTS.logit.LDV.excl <- lrm(PTS_ai_reversed ~ Lag_lognonnegUS_fdi_foodpGDP + Lag_lognonnegUS_fdi_chemicalpGDP + Lag_lognonnegUS_fdi_prim_fab_metalpGDP + Lag_lognonnegUS_fdi_machinerypGDP + Lag_lognonnegUS_fdi_electricalpGDP + Lag_lognonnegUS_fdi_transportpGDP + Lag_lognonnegUS_fdi_whole_tradepGDP + Lag_lognonnegUS_fdi_depositorypGDP+ Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_PTS_ai_reversed+factor(wb_region),data=data.PTS.excl.LDV,x=TRUE, y=TRUE)
              PTS.logit.LDV.excl.corr <- robcov(PTS.logit.LDV.excl,data.PTS.excl.LDV$country) 
              
              data.Fariss.excl.LDV <- as.data.frame(data.Fariss.excl.LDV)
              form   <- Fariss_HR ~  Lag_lognonnegUS_fdi_foodpGDP + Lag_lognonnegUS_fdi_chemicalpGDP + Lag_lognonnegUS_fdi_prim_fab_metalpGDP + Lag_lognonnegUS_fdi_machinerypGDP + Lag_lognonnegUS_fdi_electricalpGDP + Lag_lognonnegUS_fdi_transportpGDP + Lag_lognonnegUS_fdi_whole_tradepGDP + Lag_lognonnegUS_fdi_depositorypGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_Fariss_HR+factor(wb_region)
              res.po.excl <- plm(form,model="pooling",index=c("country","year"),data=data.Fariss.excl.LDV)
              summary(res.po.excl)      
              ## PCSE a la Beck-Katz
              res.BKtime.excl  <- vcovBK(x=res.po.excl,cluster="time")
              ct.time.excl     <- coeftest(res.po.excl,vcov=res.BKtime.excl)
              print(ct.time.excl)
              
              # rename lagged values to get them into one row
              names(CIRI.logit.LDV.excl.corr$coefficients)
              names(CIRI.logit.LDV.excl.corr$coefficients)[names(CIRI.logit.LDV.excl.corr$coefficients) == "Lag_CIRI_PHYSINT"] <- "Past"
              rownames(CIRI.logit.LDV.excl.corr$var)[rownames(CIRI.logit.LDV.excl.corr$var)== "Lag_CIRI_PHYSINT"] <- "Past"
              colnames(CIRI.logit.LDV.excl.corr$var)[colnames(CIRI.logit.LDV.excl.corr$var)== "Lag_CIRI_PHYSINT"] <- "Past"
              names(PTS.logit.LDV.excl.corr$coefficients)
              names(PTS.logit.LDV.excl.corr$coefficients)[names(PTS.logit.LDV.excl.corr$coefficients) == "Lag_PTS_ai_reversed"] <- "Past"
              rownames(PTS.logit.LDV.excl.corr$var)[rownames(PTS.logit.LDV.excl.corr$var)== "Lag_PTS_ai_reversed"] <- "Past"
              colnames(PTS.logit.LDV.excl.corr$var)[colnames(PTS.logit.LDV.excl.corr$var)== "Lag_PTS_ai_reversed"] <- "Past"
              
              
              res.po.excl$vcov <- res.BKtime.excl  # replacing the first uncorrected residual covariance structure with the corrected one see str()
              names(res.po.excl$coefficients)[names(res.po.excl$coefficients) == "Lag_Fariss_HR"] <- "Past"
              rownames(res.po.excl$vcov)[rownames(res.po.excl$vcov)== "Lag_Fariss_HR"] <- "Past"
              colnames(res.po.excl$vcov)[colnames(res.po.excl$vcov)== "Lag_Fariss_HR"] <- "Past"
              
              names(res.po.excl$coefficients)[names(res.po.excl$coefficients) == "factor(wb_region)ECA"] <- "wb_region=ECA"
              names(res.po.excl$coefficients)[names(res.po.excl$coefficients) == "factor(wb_region)LAC"] <- "wb_region=LAC"
              names(res.po.excl$coefficients)[names(res.po.excl$coefficients) == "factor(wb_region)MNA"] <- "wb_region=MNA"
              names(res.po.excl$coefficients)[names(res.po.excl$coefficients) == "factor(wb_region)SAS"] <- "wb_region=SAS"
              names(res.po.excl$coefficients)[names(res.po.excl$coefficients) == "factor(wb_region)SSA"] <- "wb_region=SSA"
              names(res.po.excl$coefficients)[names(res.po.excl$coefficients) == "factor(wb_region)EAP"] <- "wb_region=EAP"
              rownames(res.po.excl$vcov)[rownames(res.po.excl$vcov)== "factor(wb_region)ECA"] <- "wb_region=ECA"
              colnames(res.po.excl$vcov)[colnames(res.po.excl$vcov)== "factor(wb_region)ECA"] <- "wb_region=ECA"
              rownames(res.po.excl$vcov)[rownames(res.po.excl$vcov)== "factor(wb_region)LAC"] <- "wb_region=LAC"
              colnames(res.po.excl$vcov)[colnames(res.po.excl$vcov)== "factor(wb_region)LAC"] <- "wb_region=LAC"
              rownames(res.po.excl$vcov)[rownames(res.po.excl$vcov)== "factor(wb_region)MNA"] <- "wb_region=MNA"
              colnames(res.po.excl$vcov)[colnames(res.po.excl$vcov)== "factor(wb_region)MNA"] <- "wb_region=MNA"
              rownames(res.po.excl$vcov)[rownames(res.po.excl$vcov)== "factor(wb_region)SAS"] <- "wb_region=SAS"
              colnames(res.po.excl$vcov)[colnames(res.po.excl$vcov)== "factor(wb_region)SAS"] <- "wb_region=SAS"
              rownames(res.po.excl$vcov)[rownames(res.po.excl$vcov)== "factor(wb_region)SSA"] <- "wb_region=SSA"
              colnames(res.po.excl$vcov)[colnames(res.po.excl$vcov)== "factor(wb_region)SSA"] <- "wb_region=SSA"
              rownames(res.po.excl$vcov)[rownames(res.po.excl$vcov)== "factor(wb_region)EAP"] <- "wb_region=EAP"
              colnames(res.po.excl$vcov)[colnames(res.po.excl$vcov)== "factor(wb_region)EAP"] <- "wb_region=EAP"
              
varnames <- c("Food FDI","Chemical FDI","Metal FDI","Machinery FDI","Electrical FDI","Transport FDI", "Wholesale FDI","Depository FDI", "Trade", "GDP p. capita", "Population", "Democracy", "Conflict","Past", "East Asia and Pacific", "Latin America and Caribbean","Middle East and North Africa","South Asia", "Sub-Saharan Africa")
dvnames <- c("CIRI Integrity","PTS Amnesty","Latent Scores")
stargazer(CIRI.logit.LDV.excl.corr, PTS.logit.LDV.excl.corr,res.po.excl,
          dep.var.labels=dvnames,
        covariate.labels=varnames,
          omit=c("y>=1","y>=2","y>=3","y>=4","y>=5","y>=6","y>=7","y>=8","factor","Constant","(Intercept)"),
          type="text",omit.stat=c("chi2","f","ser"),font.size="scriptsize",notes.align="l",title="Sectoral FDI and Personal Integrity Rights, incl. region dummies",model.numbers= FALSE,model.names = FALSE,label="sectoral.region.dummies.tex",out="sectoral.region.dummies.tex") # suppressing model numbers for more space





# # # # # # # # # # # # # # # # # # # # # # # ## # # # # # # # # # # # # # # # # # # # # # # #
# Table "Total FDI and Personal Integrity Rights Protection (OLS with country dummies)" ####
# # # # # # # # # # # # # # # # # # # # # # # ## # # # # # # # # # # # # # # # # # # # # # # #



# data
data.CIRI.PHY.LDV <- na.omit(t.nono[,cbind("CIRI_PHYSINT","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_totalpGDP","Lag_CIRI_PHYSINT", "country","year")])
data.PTS.LDV <- na.omit(t.nono[,cbind("PTS_ai_reversed","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_totalpGDP", "Lag_PTS_ai_reversed", "country","year")])
data.Fariss.LDV <- na.omit(t.nono[,cbind("Fariss_HR","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_totalpGDP", "Lag_Fariss_HR","country","year")]) 
data.Fariss.LDV$country <- factor(as.character(data.Fariss.LDV$country)) #from 192 to 118

# For total UN FDI:
data.CIRI.PHY1.LDV <- na.omit(t.nono[,cbind("CIRI_PHYSINT","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_log_UN_FDI_stock_pgdp" ,"Lag_CIRI_PHYSINT","country","year")])
data.PTS1.LDV <- na.omit(t.nono[,cbind("PTS_ai_reversed","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_log_UN_FDI_stock_pgdp","Lag_PTS_ai_reversed", "country","year")])
data.Fariss1.LDV <- na.omit(t.nono[,cbind("Fariss_HR","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_log_UN_FDI_stock_pgdp", "Lag_Fariss_HR","country","year")])


data.CIRI.PHY.excl.LDV <- na.omit(t.nono[,cbind("CIRI_PHYSINT","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_foodpGDP","Lag_lognonnegUS_fdi_chemicalpGDP","Lag_lognonnegUS_fdi_prim_fab_metalpGDP","Lag_lognonnegUS_fdi_machinerypGDP","Lag_lognonnegUS_fdi_electricalpGDP","Lag_lognonnegUS_fdi_transportpGDP","Lag_lognonnegUS_fdi_whole_tradepGDP","Lag_lognonnegUS_fdi_depositorypGDP","Lag_CIRI_PHYSINT", "country","year")])
data.PTS.excl.LDV  <- na.omit(t.nono[,cbind("PTS_ai_reversed","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_foodpGDP","Lag_lognonnegUS_fdi_chemicalpGDP","Lag_lognonnegUS_fdi_prim_fab_metalpGDP","Lag_lognonnegUS_fdi_machinerypGDP","Lag_lognonnegUS_fdi_electricalpGDP","Lag_lognonnegUS_fdi_transportpGDP","Lag_lognonnegUS_fdi_whole_tradepGDP","Lag_lognonnegUS_fdi_depositorypGDP","Lag_PTS_ai_reversed","country","year")])
data.Fariss.excl.LDV <- na.omit(t.nono[,cbind("Fariss_HR","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_foodpGDP","Lag_lognonnegUS_fdi_chemicalpGDP","Lag_lognonnegUS_fdi_prim_fab_metalpGDP","Lag_lognonnegUS_fdi_machinerypGDP","Lag_lognonnegUS_fdi_electricalpGDP","Lag_lognonnegUS_fdi_transportpGDP","Lag_lognonnegUS_fdi_whole_tradepGDP","Lag_lognonnegUS_fdi_depositorypGDP","Lag_Fariss_HR","country","year")])


# US FDI
CIRI.fe <- as.data.frame(data.CIRI.PHY.LDV)
form   <- CIRI_PHYSINT ~ Lag_lognonnegUS_fdi_totalpGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_CIRI_PHYSINT +factor(country) 
res.po.CIRI.fe <- plm(form,model="pooling",index=c("country","year"),data=data.CIRI.PHY.LDV)
summary(res.po.CIRI.fe)     
res.BKtime.CIRI.fe  <- vcovBK(x=res.po.CIRI.fe,cluster="time")
ct.time.CIRI.fe     <- coeftest(res.po.CIRI.fe,vcov=res.BKtime.CIRI.fe)
print(ct.time.CIRI.fe)


PTS.fe <- as.data.frame(data.PTS.LDV)
form   <- PTS_ai_reversed ~ Lag_lognonnegUS_fdi_totalpGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_PTS_ai_reversed + factor(country)
res.po.PTS.fe <- plm(form,model="pooling",index=c("country","year"),data=data.PTS.LDV)
summary(res.po.PTS.fe)     
res.BKtime.PTS.fe  <- vcovBK(x=res.po.PTS.fe,cluster="time")
ct.time.PTS.fe     <- coeftest(res.po.PTS.fe,vcov=res.BKtime.PTS.fe)
print(ct.time.PTS.fe)

data.Fariss.LDV <- as.data.frame(data.Fariss.LDV)
form   <- Fariss_HR ~ Lag_lognonnegUS_fdi_totalpGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_Fariss_HR + factor(country)  
res.po <- plm(form,model="pooling",index=c("country","year"),data=data.Fariss.LDV)
summary(res.po)      
res.BKtime  <- vcovBK(x=res.po,cluster="time")
ct.time     <- coeftest(res.po,vcov=res.BKtime)
print(ct.time)

# rename lagged values to get them into one row with stargazer; replace plm se's with corrected ones
res.po.CIRI.fe$vcov <- res.BKtime.CIRI.fe  # replacing the first uncorrected residual covariance structure with the corrected one see str()
names(res.po.CIRI.fe$coefficients)[names(res.po.CIRI.fe$coefficients) == "Lag_CIRI_PHYSINT"] <- "Past"
rownames(res.po.CIRI.fe$vcov)[rownames(res.po.CIRI.fe$vcov)== "Lag_CIRI_PHYSINT"] <- "Past"
colnames(res.po.CIRI.fe$vcov)[colnames(res.po.CIRI.fe$vcov)== "Lag_CIRI_PHYSINT"] <- "Past"

res.po.PTS.fe$vcov <- res.BKtime.PTS.fe  # replacing the first uncorrected residual covariance structure with the corrected one see str()
names(res.po.PTS.fe$coefficients)[names(res.po.PTS.fe$coefficients) == "Lag_PTS_ai_reversed"] <- "Past"
rownames(res.po.PTS.fe$vcov)[rownames(res.po.PTS.fe$vcov)== "Lag_PTS_ai_reversed"] <- "Past"
colnames(res.po.PTS.fe$vcov)[colnames(res.po.PTS.fe$vcov)== "Lag_PTS_ai_reversed"] <- "Past"

res.po$vcov <- res.BKtime  # replacing the first uncorrected residual covariance structure with the corrected one see str()
names(res.po$coefficients)[names(res.po$coefficients) == "Lag_Fariss_HR"] <- "Past"
rownames(res.po$vcov)[rownames(res.po$vcov)== "Lag_Fariss_HR"] <- "Past"
colnames(res.po$vcov)[colnames(res.po$vcov)== "Lag_Fariss_HR"] <- "Past"

# Results Total UN FDI 
data.CIRI.PHY1.LDV <- as.data.frame(data.CIRI.PHY1.LDV)
form   <- CIRI_PHYSINT ~ Lag_log_UN_FDI_stock_pgdp + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_CIRI_PHYSINT +factor(country)#-1 
res.po.CIRI.fe1 <- plm(form,model="pooling",index=c("country","year"),data=data.CIRI.PHY1.LDV)
summary(res.po.CIRI.fe1)     
res.BKtime.CIRI.fe1  <- vcovBK(x=res.po.CIRI.fe1,cluster="time")
ct.time.CIRI.fe1     <- coeftest(res.po.CIRI.fe1,vcov=res.BKtime.CIRI.fe1)
print(ct.time.CIRI.fe1)

# For total UN FDI:
data.PTS1.LDV <- as.data.frame(data.PTS1.LDV)
form   <- PTS_ai_reversed ~ Lag_log_UN_FDI_stock_pgdp + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_PTS_ai_reversed + factor(country)
res.po.PTS.fe1 <- plm(form,model="pooling",index=c("country","year"),data=data.PTS1.LDV)
summary(res.po.PTS.fe1)     
res.BKtime.PTS.fe1  <- vcovBK(x=res.po.PTS.fe1,cluster="time")
ct.time.PTS.fe1     <- coeftest(res.po.PTS.fe1,vcov=res.BKtime.PTS.fe1)
print(ct.time.PTS.fe1)

data.Fariss.1.LDV <- as.data.frame(data.Fariss1.LDV)
form   <- Fariss_HR ~ Lag_log_UN_FDI_stock_pgdp + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl +Lag_Fariss_HR + factor(country) 
res.po1 <- plm(form,model="pooling",index=c("country","year"),data=data.Fariss1.LDV)
summary(res.po1)      
res.BKtime1  <- vcovBK(x=res.po1,cluster="time")
ct.time1     <- coeftest(res.po1,vcov=res.BKtime1)
print(ct.time1) 

# rename lagged values to get them into one row
res.po.CIRI.fe1$vcov <- res.BKtime.CIRI.fe1  # replacing the first uncorrected residual covariance structure with the corrected one see str()
names(res.po.CIRI.fe1$coefficients)[names(res.po.CIRI.fe1$coefficients) == "Lag_CIRI_PHYSINT"] <- "Past"
rownames(res.po.CIRI.fe1$vcov)[rownames(res.po.CIRI.fe1$vcov)== "Lag_CIRI_PHYSINT"] <- "Past"
colnames(res.po.CIRI.fe1$vcov)[colnames(res.po.CIRI.fe1$vcov)== "Lag_CIRI_PHYSINT"] <- "Past"

res.po.PTS.fe1$vcov <- res.BKtime.PTS.fe1  # replacing the first uncorrected residual covariance structure with the corrected one see str()
names(res.po.PTS.fe1$coefficients)[names(res.po.PTS.fe1$coefficients) == "Lag_PTS_ai_reversed"] <- "Past"
rownames(res.po.PTS.fe1$vcov)[rownames(res.po.PTS.fe1$vcov)== "Lag_PTS_ai_reversed"] <- "Past"
colnames(res.po.PTS.fe1$vcov)[colnames(res.po.PTS.fe1$vcov)== "Lag_PTS_ai_reversed"] <- "Past"

res.po1$vcov <- res.BKtime1  # replacing the first uncorrected residual covariance structure with the corrected one see str()
names(res.po1$coefficients)[names(res.po1$coefficients) == "Lag_Fariss_HR"] <- "Past"
rownames(res.po1$vcov)[rownames(res.po1$vcov)== "Lag_Fariss_HR"] <- "Past"
colnames(res.po1$vcov)[colnames(res.po1$vcov)== "Lag_Fariss_HR"] <- "Past"

#?table stargazer
varnames <- c("Global FDI p. GDP","US FDI p. GDP", "Trade", "GDP p. capita", "Population", "Democracy", "Conflict","Past")
dvnames <- c("CIRI Integrity","PTS Amnesty","Latent Scores","CIRI Integrity","PTS Amnesty","Latent Scores")
stargazer(res.po.CIRI.fe1, res.po.PTS.fe1,res.po1,res.po.CIRI.fe, res.po.PTS.fe,res.po,
          dep.var.labels=dvnames,
          covariate.labels=varnames,
          omit=c("y>=1","y>=2","y>=3","y>=4","y>=5","y>=6","y>=7","y>=8","factor","Constant","(Intercept)"),
          type="text",omit.stat=c("chi2","f","ser"),model.names=T,notes=c("OLS with panel corrected standard errors (PCSE) in parentheses below the coefficient estimates.","FDIpGDP, GDPpc, population, trade are logged. Past is the lagged dependent variable (LDV).","Country dummies not displayed."),font.size="scriptsize",notes.align="l",title="Total FDI and Personal Integrity Rights Protection (OLS with country dummies)",label="fe.tex",out="fe.tex")






# # # # # # # # # # # # # # # # # # # # # # # ## # # # # # # # # # # # # # # # # # # # # # # #
# Table "Sectoral U.S. FDI and Personal Integrity Rights Protection (OLS with country dummies)" ####
# # # # # # # # # # # # # # # # # # # # # # # ## # # # # # # # # # # # # # # # # # # # # # # #

# # Sectoral (exclusive resource) FDI Models BASELINE ####
data.CIRI.PHY.excl.LDV <- as.data.frame(data.CIRI.PHY.excl.LDV)
form   <- CIRI_PHYSINT ~  Lag_lognonnegUS_fdi_foodpGDP + Lag_lognonnegUS_fdi_chemicalpGDP + Lag_lognonnegUS_fdi_prim_fab_metalpGDP + Lag_lognonnegUS_fdi_machinerypGDP + Lag_lognonnegUS_fdi_electricalpGDP + Lag_lognonnegUS_fdi_transportpGDP + Lag_lognonnegUS_fdi_whole_tradepGDP + Lag_lognonnegUS_fdi_depositorypGDP+ Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_CIRI_PHYSINT +factor(country)
res.po.CIRI.excl.fe <- plm(form,model="pooling",index=c("country","year"),data=data.CIRI.PHY.excl.LDV)
summary(res.po.CIRI.excl.fe)     
res.BKtime.CIRI.excl.fe  <- vcovBK(x=res.po.CIRI.excl.fe,cluster="time")
ct.time.CIRI.excl.fe     <- coeftest(res.po.CIRI.excl.fe,vcov=res.BKtime.CIRI.excl.fe)
print(ct.time.CIRI.excl.fe)


data.PTS.excl.LDV <- as.data.frame(data.PTS.excl.LDV)
form   <- PTS_ai_reversed ~ Lag_lognonnegUS_fdi_foodpGDP + Lag_lognonnegUS_fdi_chemicalpGDP + Lag_lognonnegUS_fdi_prim_fab_metalpGDP + Lag_lognonnegUS_fdi_machinerypGDP + Lag_lognonnegUS_fdi_electricalpGDP + Lag_lognonnegUS_fdi_transportpGDP + Lag_lognonnegUS_fdi_whole_tradepGDP + Lag_lognonnegUS_fdi_depositorypGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_PTS_ai_reversed + factor(country)
res.po.PTS.excl.fe <- plm(form,model="pooling",index=c("country","year"),data=data.PTS.excl.LDV)
summary(res.po.PTS.excl.fe)     
res.BKtime.PTS.excl.fe  <- vcovBK(x=res.po.PTS.excl.fe,cluster="time")
ct.time.PTS.excl.fe     <- coeftest(res.po.PTS.excl.fe,vcov=res.BKtime.PTS.excl.fe)
print(ct.time.PTS.excl.fe)

data.Fariss.excl.LDV <- as.data.frame(data.Fariss.excl.LDV)
form   <- Fariss_HR ~  Lag_lognonnegUS_fdi_foodpGDP + Lag_lognonnegUS_fdi_chemicalpGDP + Lag_lognonnegUS_fdi_prim_fab_metalpGDP + Lag_lognonnegUS_fdi_machinerypGDP + Lag_lognonnegUS_fdi_electricalpGDP + Lag_lognonnegUS_fdi_transportpGDP + Lag_lognonnegUS_fdi_whole_tradepGDP + Lag_lognonnegUS_fdi_depositorypGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_Fariss_HR + factor(country)
res.po.excl.fe <- plm(form,model="pooling",index=c("country","year"),data=data.Fariss.excl.LDV)
summary(res.po.excl.fe)      
res.BKtime.excl.fe  <- vcovBK(x=res.po.excl.fe,cluster="time")
ct.time.excl.fe     <- coeftest(res.po.excl.fe,vcov=res.BKtime.excl.fe)
print(ct.time.excl.fe)

# rename lagged values to get them into one row
res.po.CIRI.excl.fe$vcov <- res.BKtime.CIRI.excl.fe  # replacing the first uncorrected residual covariance structure with the corrected one see str()
names(res.po.CIRI.excl.fe$coefficients)[names(res.po.CIRI.excl.fe$coefficients) == "Lag_CIRI_PHYSINT"] <- "Past"
rownames(res.po.CIRI.excl.fe$vcov)[rownames(res.po.CIRI.excl.fe$vcov)== "Lag_CIRI_PHYSINT"] <- "Past"
colnames(res.po.CIRI.excl.fe$vcov)[colnames(res.po.CIRI.excl.fe$vcov)== "Lag_CIRI_PHYSINT"] <- "Past"

res.po.PTS.excl.fe$vcov <- res.BKtime.PTS.excl.fe  # replacing the first uncorrected residual covariance structure with the corrected one see str()
names(res.po.PTS.excl.fe$coefficients)[names(res.po.PTS.excl.fe$coefficients) == "Lag_PTS_ai_reversed"] <- "Past"
rownames(res.po.PTS.excl.fe$vcov)[rownames(res.po.PTS.excl.fe$vcov)== "Lag_PTS_ai_reversed"] <- "Past"
colnames(res.po.PTS.excl.fe$vcov)[colnames(res.po.PTS.excl.fe$vcov)== "Lag_PTS_ai_reversed"] <- "Past"

res.po.excl.fe$vcov <- res.BKtime.excl.fe  # replacing the first uncorrected residual covariance structure with the corrected one see str()
names(res.po.excl.fe$coefficients)[names(res.po.excl.fe$coefficients) == "Lag_Fariss_HR"] <- "Past"
rownames(res.po.excl.fe$vcov)[rownames(res.po.excl.fe$vcov)== "Lag_Fariss_HR"] <- "Past"
colnames(res.po.excl.fe$vcov)[colnames(res.po.excl.fe$vcov)== "Lag_Fariss_HR"] <- "Past"

#?table stargazer
varnames <- c("Food FDI","Chemical FDI","Metal FDI","Machinery FDI","Electrical FDI","Transport FDI", "Wholesale FDI","Depository FDI","Trade", "GDP p. capita", "Population", "Democracy", "Conflict","Past")
dvnames <- c("CIRI Integrity","PTS Amnesty","Latent Scores")
stargazer(res.po.CIRI.excl.fe, res.po.PTS.excl.fe,res.po.excl.fe,
          dep.var.labels=dvnames,
          covariate.labels=varnames,
          omit=c("y>=1","y>=2","y>=3","y>=4","y>=5","y>=6","y>=7","y>=8","factor","Constant","(Intercept)"),
          type="text",omit.stat=c("chi2","f","ser"),model.names=T,notes=c("Country dummies not displayed."),font.size="scriptsize",notes.align="l",title="Sectoral U.S. FDI and Personal Integrity Rights Protection (OLS with country dummies)",label="sectoral.fe.tex",out="sectoral.fe.tex")










##############################################################################################
# Creating table: "Total FDI and Forms of Repression (including civil war)"      ######
# "Lag_conf_dom_min","Lag_conf_dom_maj"  
##############################################################################################

dim(t.nono) 

# data ##########
# for total U.S. FDI:
data.CIRI.PHY.LDV <- na.omit(t.nono[,cbind("CIRI_PHYSINT","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_conf_dom_min","Lag_conf_dom_maj" ,"Lag_lognonnegUS_fdi_totalpGDP","Lag_CIRI_PHYSINT", "country","year")])
data.PTS.LDV <- na.omit(t.nono[,cbind("PTS_ai_reversed","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_conf_dom_min","Lag_conf_dom_maj" ,"Lag_lognonnegUS_fdi_totalpGDP", "Lag_PTS_ai_reversed", "country","year")])
data.Fariss.LDV <- na.omit(t.nono[,cbind("Fariss_HR","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_conf_dom_min","Lag_conf_dom_maj" ,"Lag_lognonnegUS_fdi_totalpGDP", "Lag_Fariss_HR","country","year")]) 
data.Fariss.LDV$country <- factor(as.character(data.Fariss.LDV$country)) #
# For total UN FDI:
data.CIRI.PHY1.LDV <- na.omit(t.nono[,cbind("CIRI_PHYSINT","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_conf_dom_min","Lag_conf_dom_maj" ,"Lag_log_UN_FDI_stock_pgdp" ,"Lag_CIRI_PHYSINT","country","year")])
data.PTS1.LDV <- na.omit(t.nono[,cbind("PTS_ai_reversed","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_conf_dom_min","Lag_conf_dom_maj" ,"Lag_log_UN_FDI_stock_pgdp","Lag_PTS_ai_reversed", "country","year")])
data.Fariss1.LDV <- na.omit(t.nono[,cbind("Fariss_HR","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_conf_dom_min","Lag_conf_dom_maj" ,"Lag_log_UN_FDI_stock_pgdp", "Lag_Fariss_HR","country","year")])


# for sectoral (exclusive)
data.CIRI.PHY.excl.LDV <- na.omit(t.nono[,cbind("CIRI_PHYSINT","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_conf_dom_min","Lag_conf_dom_maj" ,"Lag_lognonnegUS_fdi_foodpGDP","Lag_lognonnegUS_fdi_chemicalpGDP","Lag_lognonnegUS_fdi_prim_fab_metalpGDP","Lag_lognonnegUS_fdi_machinerypGDP","Lag_lognonnegUS_fdi_electricalpGDP","Lag_lognonnegUS_fdi_transportpGDP","Lag_lognonnegUS_fdi_whole_tradepGDP","Lag_lognonnegUS_fdi_depositorypGDP","Lag_CIRI_PHYSINT", "country","year")])
data.PTS.excl.LDV  <- na.omit(t.nono[,cbind("PTS_ai_reversed","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_conf_dom_min","Lag_conf_dom_maj" ,"Lag_lognonnegUS_fdi_foodpGDP","Lag_lognonnegUS_fdi_chemicalpGDP","Lag_lognonnegUS_fdi_prim_fab_metalpGDP","Lag_lognonnegUS_fdi_machinerypGDP","Lag_lognonnegUS_fdi_electricalpGDP","Lag_lognonnegUS_fdi_transportpGDP","Lag_lognonnegUS_fdi_whole_tradepGDP","Lag_lognonnegUS_fdi_depositorypGDP","Lag_PTS_ai_reversed","country","year")])
data.Fariss.excl.LDV <- na.omit(t.nono[,cbind("Fariss_HR","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_conf_dom_min","Lag_conf_dom_maj" ,"Lag_lognonnegUS_fdi_foodpGDP","Lag_lognonnegUS_fdi_chemicalpGDP","Lag_lognonnegUS_fdi_prim_fab_metalpGDP","Lag_lognonnegUS_fdi_machinerypGDP","Lag_lognonnegUS_fdi_electricalpGDP","Lag_lognonnegUS_fdi_transportpGDP","Lag_lognonnegUS_fdi_whole_tradepGDP","Lag_lognonnegUS_fdi_depositorypGDP","Lag_Fariss_HR","country","year")])

#?Total models
CIRI.logit.LDV <- lrm(CIRI_PHYSINT ~ Lag_lognonnegUS_fdi_totalpGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_conf_dom_min + Lag_conf_dom_maj  + Lag_CIRI_PHYSINT,data=data.CIRI.PHY.LDV,x=TRUE, y=TRUE)
CIRI.logit.LDV.corr <- robcov(CIRI.logit.LDV,data.CIRI.PHY.LDV$country) #robust errors  

PTS.logit.LDV <- lrm(PTS_ai_reversed ~ Lag_lognonnegUS_fdi_totalpGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_conf_dom_min + Lag_conf_dom_maj + Lag_PTS_ai_reversed,data=data.PTS.LDV,x=TRUE, y=TRUE)
PTS.logit.LDV.corr <- robcov(PTS.logit.LDV,data.PTS.LDV$country) #robust errors  

#Fariss xtpcse with LDV
data.Fariss.LDV <- as.data.frame(data.Fariss.LDV)
form   <- Fariss_HR ~ Lag_lognonnegUS_fdi_totalpGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_conf_dom_min + Lag_conf_dom_maj + Lag_Fariss_HR
res.po <- plm(form,model="pooling",index=c("country","year"),data=data.Fariss.LDV)
summary(res.po)      
## PCSE a la Beck-Katz
res.BKtime  <- vcovBK(x=res.po,cluster="time")
ct.time     <- coeftest(res.po,vcov=res.BKtime)
print(ct.time)

# rename lagged values to get them into one row with stargazer; replace plm se's with corrected ones
names(CIRI.logit.LDV.corr$coefficients)
names(CIRI.logit.LDV.corr$coefficients)[names(CIRI.logit.LDV.corr$coefficients) == "Lag_CIRI_PHYSINT"] <- "Past"
rownames(CIRI.logit.LDV.corr$var)[rownames(CIRI.logit.LDV.corr$var)== "Lag_CIRI_PHYSINT"] <- "Past"
colnames(CIRI.logit.LDV.corr$var)[colnames(CIRI.logit.LDV.corr$var)== "Lag_CIRI_PHYSINT"] <- "Past"

names(PTS.logit.LDV.corr$coefficients)
names(PTS.logit.LDV.corr$coefficients)[names(PTS.logit.LDV.corr$coefficients) == "Lag_PTS_ai_reversed"] <- "Past"
rownames(PTS.logit.LDV.corr$var)[rownames(PTS.logit.LDV.corr$var)== "Lag_PTS_ai_reversed"] <- "Past"
colnames(PTS.logit.LDV.corr$var)[colnames(PTS.logit.LDV.corr$var)== "Lag_PTS_ai_reversed"] <- "Past"

res.po$vcov <- res.BKtime  # replacing the first uncorrected residual covariance structure with the corrected one see str()
names(res.po$coefficients)[names(res.po$coefficients) == "Lag_Fariss_HR"] <- "Past"
rownames(res.po$vcov)[rownames(res.po$vcov)== "Lag_Fariss_HR"] <- "Past"
colnames(res.po$vcov)[colnames(res.po$vcov)== "Lag_Fariss_HR"] <- "Past"

CIRI.logit.LDV.UN <- lrm(CIRI_PHYSINT ~ Lag_log_UN_FDI_stock_pgdp + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_conf_dom_min + Lag_conf_dom_maj + Lag_CIRI_PHYSINT,data=data.CIRI.PHY1.LDV ,x=TRUE, y=TRUE)
CIRI.logit.LDV.UN.corr <- robcov(CIRI.logit.LDV.UN,data.CIRI.PHY1.LDV$country) #robust errors 

PTS.logit.LDV.UN <- lrm(PTS_ai_reversed ~ Lag_log_UN_FDI_stock_pgdp + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_conf_dom_min + Lag_conf_dom_maj + Lag_PTS_ai_reversed,data=data.PTS1.LDV,x=TRUE, y=TRUE)
PTS.logit.LDV.UN.corr <- robcov(PTS.logit.LDV.UN,data.PTS1.LDV$country) #robust errors 

# Total FDI Fariss 
data.Fariss.1.LDV <- as.data.frame(data.Fariss1.LDV)
form   <- Fariss_HR ~ Lag_log_UN_FDI_stock_pgdp + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_conf_dom_min + Lag_conf_dom_maj + Lag_Fariss_HR
res.po1 <- plm(form,model="pooling",index=c("country","year"),data=data.Fariss1.LDV)
summary(res.po1)      
## PCSE a la Beck-Katz
res.BKtime1  <- vcovBK(x=res.po1,cluster="time")
ct.time1     <- coeftest(res.po1,vcov=res.BKtime1)
print(ct.time1) # 


# rename lagged values to get them into one row
names(CIRI.logit.LDV.UN.corr$coefficients)
names(CIRI.logit.LDV.UN.corr$coefficients)[names(CIRI.logit.LDV.UN.corr$coefficients) == "Lag_CIRI_PHYSINT"] <- "Past"
rownames(CIRI.logit.LDV.UN.corr$var)[rownames(CIRI.logit.LDV.UN.corr$var)== "Lag_CIRI_PHYSINT"] <- "Past"
colnames(CIRI.logit.LDV.UN.corr$var)[colnames(CIRI.logit.LDV.UN.corr$var)== "Lag_CIRI_PHYSINT"] <- "Past"
names(PTS.logit.LDV.UN.corr$coefficients)
names(PTS.logit.LDV.UN.corr$coefficients)[names(PTS.logit.LDV.UN.corr$coefficients) == "Lag_PTS_ai_reversed"] <- "Past"
rownames(PTS.logit.LDV.UN.corr$var)[rownames(PTS.logit.LDV.UN.corr$var)== "Lag_PTS_ai_reversed"] <- "Past"
colnames(PTS.logit.LDV.UN.corr$var)[colnames(PTS.logit.LDV.UN.corr$var)== "Lag_PTS_ai_reversed"] <- "Past"

res.po1$vcov <- res.BKtime1  # replacing the first uncorrected residual covariance structure with the corrected one see str()
names(res.po1$coefficients)[names(res.po1$coefficients) == "Lag_Fariss_HR"] <- "Past"
rownames(res.po1$vcov)[rownames(res.po1$vcov)== "Lag_Fariss_HR"] <- "Past"
colnames(res.po1$vcov)[colnames(res.po1$vcov)== "Lag_Fariss_HR"] <- "Past"

# Large Table for both Total FDI (US, UN) wih domestic war####
varnames <- c("Global FDI p.GDP","US FDI p. GDP", "Trade", "GDP p. capita", "Population", "Democracy", "Min. Dom. Confl.","Maj. Dom. Confl.","Past")
dvnames <- c("CIRI Integrity","PTS Amnesty","Latent Scores","CIRI Integrity","PTS Amnesty","Latent Scores")
stargazer(CIRI.logit.LDV.UN.corr, PTS.logit.LDV.UN.corr,res.po1,CIRI.logit.LDV.corr,PTS.logit.LDV.corr,res.po,
          dep.var.labels=dvnames,
          covariate.labels=varnames,
          omit=c("y>=1","y>=2","y>=3","y>=4","y>=5","y>=6","y>=7","y>=8","factor","Constant","(Intercept)"),
          type="text",omit.stat=c("chi2","f","ser"),notes=c("Robustness check: dummy for international and domestic conflict is replaced by major and minor domestic conflict.","Ordered logit with country-clustered standard errors.","OLS with panel corrected standard errors.","Ordered Logit Models: Nagelkerke R-squared.","FDIpGDP, GDPpc, population, trade are logged.","Past is the lagged dependent variable (LDV).","Years included: 1983-2010."),font.size="scriptsize",notes.align="l",title="Total FDI and Forms of Repression (including civil war)",label="total.civilwar.tex",out="total.civilwar.tex")



# # # #  # # # #  # # # #  # # # #  # # # #  # # # #  # # # #  # # # #  # # # #  # # # #  # # # #  # # # #  # # # #  
# Creating table "Sectoral FDI and Repression (including civil war)"
# # # #  # # # #  # # # #  # # # #  # # # #  # # # #  # # # #  # # # #  # # # #  # # # #  # # # #  # # # #  # # # #  

CIRI.logit.LDV.excl <- lrm(CIRI_PHYSINT ~ Lag_lognonnegUS_fdi_foodpGDP + Lag_lognonnegUS_fdi_chemicalpGDP + Lag_lognonnegUS_fdi_prim_fab_metalpGDP + Lag_lognonnegUS_fdi_machinerypGDP + Lag_lognonnegUS_fdi_electricalpGDP + Lag_lognonnegUS_fdi_transportpGDP + Lag_lognonnegUS_fdi_whole_tradepGDP + Lag_lognonnegUS_fdi_depositorypGDP+ Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_conf_dom_min + Lag_conf_dom_maj + Lag_CIRI_PHYSINT,data=data.CIRI.PHY.excl.LDV  ,x=TRUE, y=TRUE)
CIRI.logit.LDV.excl.corr <- robcov(CIRI.logit.LDV.excl,data.CIRI.PHY.excl.LDV$country) #robust errors  

PTS.logit.LDV.excl <- lrm(PTS_ai_reversed ~ Lag_lognonnegUS_fdi_foodpGDP + Lag_lognonnegUS_fdi_chemicalpGDP + Lag_lognonnegUS_fdi_prim_fab_metalpGDP + Lag_lognonnegUS_fdi_machinerypGDP + Lag_lognonnegUS_fdi_electricalpGDP + Lag_lognonnegUS_fdi_transportpGDP + Lag_lognonnegUS_fdi_whole_tradepGDP + Lag_lognonnegUS_fdi_depositorypGDP+ Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_conf_dom_min + Lag_conf_dom_maj + Lag_PTS_ai_reversed,data=data.PTS.excl.LDV,x=TRUE, y=TRUE)
PTS.logit.LDV.excl.corr <- robcov(PTS.logit.LDV.excl,data.PTS.excl.LDV$country) 

data.Fariss.excl.LDV <- as.data.frame(data.Fariss.excl.LDV)
form   <- Fariss_HR ~  Lag_lognonnegUS_fdi_foodpGDP + Lag_lognonnegUS_fdi_chemicalpGDP + Lag_lognonnegUS_fdi_prim_fab_metalpGDP + Lag_lognonnegUS_fdi_machinerypGDP + Lag_lognonnegUS_fdi_electricalpGDP + Lag_lognonnegUS_fdi_transportpGDP + Lag_lognonnegUS_fdi_whole_tradepGDP + Lag_lognonnegUS_fdi_depositorypGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_conf_dom_min + Lag_conf_dom_maj + Lag_Fariss_HR
res.po.excl <- plm(form,model="pooling",index=c("country","year"),data=data.Fariss.excl.LDV)
summary(res.po.excl)      
## PCSE a la Beck-Katz
res.BKtime.excl  <- vcovBK(x=res.po.excl,cluster="time")
ct.time.excl     <- coeftest(res.po.excl,vcov=res.BKtime.excl)
print(ct.time.excl)

# rename lagged values to get them into one row
names(CIRI.logit.LDV.excl.corr$coefficients)
names(CIRI.logit.LDV.excl.corr$coefficients)[names(CIRI.logit.LDV.excl.corr$coefficients) == "Lag_CIRI_PHYSINT"] <- "Past"
rownames(CIRI.logit.LDV.excl.corr$var)[rownames(CIRI.logit.LDV.excl.corr$var)== "Lag_CIRI_PHYSINT"] <- "Past"
colnames(CIRI.logit.LDV.excl.corr$var)[colnames(CIRI.logit.LDV.excl.corr$var)== "Lag_CIRI_PHYSINT"] <- "Past"
names(PTS.logit.LDV.excl.corr$coefficients)
names(PTS.logit.LDV.excl.corr$coefficients)[names(PTS.logit.LDV.excl.corr$coefficients) == "Lag_PTS_ai_reversed"] <- "Past"
rownames(PTS.logit.LDV.excl.corr$var)[rownames(PTS.logit.LDV.excl.corr$var)== "Lag_PTS_ai_reversed"] <- "Past"
colnames(PTS.logit.LDV.excl.corr$var)[colnames(PTS.logit.LDV.excl.corr$var)== "Lag_PTS_ai_reversed"] <- "Past"


res.po.excl$vcov <- res.BKtime.excl  # replacing the first uncorrected residual covariance structure with the corrected one see str()
names(res.po.excl$coefficients)[names(res.po.excl$coefficients) == "Lag_Fariss_HR"] <- "Past"
rownames(res.po.excl$vcov)[rownames(res.po.excl$vcov)== "Lag_Fariss_HR"] <- "Past"
colnames(res.po.excl$vcov)[colnames(res.po.excl$vcov)== "Lag_Fariss_HR"] <- "Past"

#large table with all sectoral models
varnames <- c("Food FDI","Chemical FDI","Metal FDI","Machinery FDI","Electrical FDI","Transport FDI", "Wholesale FDI","Depository FDI", "Trade", "GDP p. capita", "Population", "Democracy","Min. Dom. Confl.","Maj. Dom. Confl.","Past")
dvnames <- c("CIRI Integrity","PTS Amnesty","Latent Scores","CIRI Integrity","PTS Amnesty","Latent Scores")

stargazer(CIRI.logit.LDV.excl.corr,PTS.logit.LDV.excl.corr,res.po.excl, 
          covariate.labels=varnames, dep.var.labels=dvnames, 
          omit=c("y>=1","y>=2","y>=3","y>=4","y>=5","y>=6","y>=7","y>=8","factor","Constant","(Intercept)"), 
          omit.stat = c("f","ser","chi2"),
          nobs=T,title="Sectoral FDI and Repression (including civil war)",no.space=T,font.size="scriptsize", notes=c("Overall conflict  replaced by major and minor domestic conflict.","Ordered logit with country-clustered standard errors.","OLS with panel corrected standard errors.","Ordered Logit Models: Nagelkerke R-squared.","FDIpGDP, GDPpc, population, trade are logged.","Past is the lagged dependent variable (LDV)."),notes.align="l",type="text",label="Sectoral.civilwar.tex",out="Sectoral.civilwar.tex")







# # # # # # # # # # # # # # # # ## # # # # # 
# Creating tables "Binary Decomposition of Democracy (CIRI and PTS)" and "Binary Decomposition of Democracy (Latent Scores by Fariss)"
# only done for total models
# # # # # # # # # # # # # # # # ## # # # # # 

# use same data as main total models; but factor(Lag_polity2)  instead of Lag_polity2
# for total U.S. FDI:
data.CIRI.PHY.LDV <- na.omit(t.nono[,cbind("CIRI_PHYSINT","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_totalpGDP","Lag_CIRI_PHYSINT", "country","year")])
data.PTS.LDV <- na.omit(t.nono[,cbind("PTS_ai_reversed","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_totalpGDP", "Lag_PTS_ai_reversed", "country","year")])
data.Fariss.LDV <- na.omit(t.nono[,cbind("Fariss_HR","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_totalpGDP", "Lag_Fariss_HR","country","year")]) 
data.Fariss.LDV$country <- factor(as.character(data.Fariss.LDV$country)) 
# For total UN FDI:
data.CIRI.PHY1.LDV <- na.omit(t.nono[,cbind("CIRI_PHYSINT","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_log_UN_FDI_stock_pgdp" ,"Lag_CIRI_PHYSINT","country","year")])
data.PTS1.LDV <- na.omit(t.nono[,cbind("PTS_ai_reversed","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_log_UN_FDI_stock_pgdp","Lag_PTS_ai_reversed", "country","year")])
data.Fariss1.LDV <- na.omit(t.nono[,cbind("Fariss_HR","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_log_UN_FDI_stock_pgdp", "Lag_Fariss_HR","country","year")])

#models total US
CIRI.logit.LDV <- lrm(CIRI_PHYSINT ~ Lag_lognonnegUS_fdi_totalpGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + factor(Lag_polity2) + Lag_confl + Lag_CIRI_PHYSINT,data=data.CIRI.PHY.LDV,x=TRUE, y=TRUE)
CIRI.logit.LDV.corr <- robcov(CIRI.logit.LDV,data.CIRI.PHY.LDV$country) #robust errors  

PTS.logit.LDV <- lrm(PTS_ai_reversed ~ Lag_lognonnegUS_fdi_totalpGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + factor(Lag_polity2) + Lag_confl + Lag_PTS_ai_reversed,data=data.PTS.LDV,x=TRUE, y=TRUE)
PTS.logit.LDV.corr <- robcov(PTS.logit.LDV,data.PTS.LDV$country) #robust errors  

#Fariss xtpcse with LDV
data.Fariss.LDV <- as.data.frame(data.Fariss.LDV)
form   <- Fariss_HR ~ Lag_lognonnegUS_fdi_totalpGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + factor(Lag_polity2) + Lag_confl + Lag_Fariss_HR
res.po <- plm(form,model="pooling",index=c("country","year"),data=data.Fariss.LDV)
summary(res.po)      
## PCSE a la Beck-Katz
res.BKtime  <- vcovBK(x=res.po,cluster="time")
ct.time     <- coeftest(res.po,vcov=res.BKtime)
print(ct.time)

# rename lagged values to get them into one row with stargazer; replace plm se's with corrected ones
names(CIRI.logit.LDV.corr$coefficients)
names(CIRI.logit.LDV.corr$coefficients)[names(CIRI.logit.LDV.corr$coefficients) == "Lag_CIRI_PHYSINT"] <- "Past"
rownames(CIRI.logit.LDV.corr$var)[rownames(CIRI.logit.LDV.corr$var)== "Lag_CIRI_PHYSINT"] <- "Past"
colnames(CIRI.logit.LDV.corr$var)[colnames(CIRI.logit.LDV.corr$var)== "Lag_CIRI_PHYSINT"] <- "Past"

names(PTS.logit.LDV.corr$coefficients)
names(PTS.logit.LDV.corr$coefficients)[names(PTS.logit.LDV.corr$coefficients) == "Lag_PTS_ai_reversed"] <- "Past"
rownames(PTS.logit.LDV.corr$var)[rownames(PTS.logit.LDV.corr$var)== "Lag_PTS_ai_reversed"] <- "Past"
colnames(PTS.logit.LDV.corr$var)[colnames(PTS.logit.LDV.corr$var)== "Lag_PTS_ai_reversed"] <- "Past"

res.po$vcov <- res.BKtime  # replacing the first uncorrected residual covariance structure with the corrected one see str()
names(res.po$coefficients)[names(res.po$coefficients) == "Lag_Fariss_HR"] <- "Past"
rownames(res.po$vcov)[rownames(res.po$vcov)== "Lag_Fariss_HR"] <- "Past"
colnames(res.po$vcov)[colnames(res.po$vcov)== "Lag_Fariss_HR"] <- "Past"

# Results total UN####
CIRI.logit.LDV.UN <- lrm(CIRI_PHYSINT ~ Lag_log_UN_FDI_stock_pgdp + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + factor(Lag_polity2) + Lag_confl + Lag_CIRI_PHYSINT,data=data.CIRI.PHY1.LDV ,x=TRUE, y=TRUE)
CIRI.logit.LDV.UN.corr <- robcov(CIRI.logit.LDV.UN,data.CIRI.PHY1.LDV$country) #robust errors clustered by country 

PTS.logit.LDV.UN <- lrm(PTS_ai_reversed ~ Lag_log_UN_FDI_stock_pgdp + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + factor(Lag_polity2) + Lag_confl + Lag_PTS_ai_reversed,data=data.PTS1.LDV,x=TRUE, y=TRUE)
PTS.logit.LDV.UN.corr <- robcov(PTS.logit.LDV.UN,data.PTS1.LDV$country) #robust errors clustered by country 

# Total FDI Fariss 
data.Fariss.1.LDV <- as.data.frame(data.Fariss1.LDV)
form   <- Fariss_HR ~ Lag_log_UN_FDI_stock_pgdp + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + factor(Lag_polity2) + Lag_confl + Lag_Fariss_HR
res.po1 <- plm(form,model="pooling",index=c("country","year"),data=data.Fariss1.LDV)
summary(res.po1)      
## PCSE a la Beck-Katz
res.BKtime1  <- vcovBK(x=res.po1,cluster="time")
ct.time1     <- coeftest(res.po1,vcov=res.BKtime1)
print(ct.time1) #


# rename lagged values to get them into one row
names(CIRI.logit.LDV.UN.corr$coefficients)
names(CIRI.logit.LDV.UN.corr$coefficients)[names(CIRI.logit.LDV.UN.corr$coefficients) == "Lag_CIRI_PHYSINT"] <- "Past"
rownames(CIRI.logit.LDV.UN.corr$var)[rownames(CIRI.logit.LDV.UN.corr$var)== "Lag_CIRI_PHYSINT"] <- "Past"
colnames(CIRI.logit.LDV.UN.corr$var)[colnames(CIRI.logit.LDV.UN.corr$var)== "Lag_CIRI_PHYSINT"] <- "Past"
names(PTS.logit.LDV.UN.corr$coefficients)
names(PTS.logit.LDV.UN.corr$coefficients)[names(PTS.logit.LDV.UN.corr$coefficients) == "Lag_PTS_ai_reversed"] <- "Past"
rownames(PTS.logit.LDV.UN.corr$var)[rownames(PTS.logit.LDV.UN.corr$var)== "Lag_PTS_ai_reversed"] <- "Past"
colnames(PTS.logit.LDV.UN.corr$var)[colnames(PTS.logit.LDV.UN.corr$var)== "Lag_PTS_ai_reversed"] <- "Past"

res.po1$vcov <- res.BKtime1  # replacing the first uncorrected residual covariance structure with the corrected one see str()
names(res.po1$coefficients)[names(res.po1$coefficients) == "Lag_Fariss_HR"] <- "Past"
rownames(res.po1$vcov)[rownames(res.po1$vcov)== "Lag_Fariss_HR"] <- "Past"
colnames(res.po1$vcov)[colnames(res.po1$vcov)== "Lag_Fariss_HR"] <- "Past"

# large results table
# only CIRI and PTS (same way to name the factors; easier to rename variables)
dvnames <- c("CIRI Integrity","PTS Amnesty","CIRI Integrity","PTS Amnesty")
varnames <- c("Global FDI p.GDP","U.S. FDI p.GDP" ,"Trade", "GDP p. capita", "Population", 
              "Democracy=-9",
              "Democracy=-8",
              "Democracy=-7",
              "Democracy=-6",
              "Democracy=-5",
              "Democracy=-4",
              "Democracy=-3",
              "Democracy=-2",
              "Democracy=-1",
              "Democracy=-0",
              "Democracy=1",
              "Democracy=2",
              "Democracy=3",
              "Democracy=4",
              "Democracy=5",
              "Democracy=6",
              "Democracy=7",
              "Democracy=8",
              "Democracy=9",
              "Democracy=10",
              "Conflict","Past")
stargazer(CIRI.logit.LDV.UN.corr, PTS.logit.LDV.UN.corr,CIRI.logit.LDV.corr,PTS.logit.LDV.corr,
          dep.var.labels=dvnames,
          covariate.labels=varnames,
          omit=c("y>=1","y>=2","y>=3","y>=4","y>=5","y>=6","y>=7","y>=8","Constant","(Intercept)"),
          type="text",omit.stat=c("chi2","f","ser"),notes=c("Ordered logit with country-clustered standard errors.","FDIpGDP, GDPpc, population, trade are logged.","Years included: 1983-2010."),font.size="scriptsize",notes.align="l",title="Binary Decomposition of Democracy (CIRI and PTS)",label="total.BinDecompCIRIPTS.tex",out="total.BinDecompCIRIPTS.tex",no.space=T)


#?table only Fariss #
dvnames <- c("Latent Scores","Latent Scores")
varnames <- c("Global FDI p.GDP","U.S. FDI p.GDP" ,"Trade", "GDP p. capita", "Population", 
              "Democracy=-9",
              "Democracy=-8",
              "Democracy=-7",
              "Democracy=-6",
              "Democracy=-5",
              "Democracy=-4",
              "Democracy=-3",
              "Democracy=-2",
              "Democracy=-1",
              "Democracy=-0",
              "Democracy=1",
              "Democracy=2",
              "Democracy=3",
              "Democracy=4",
              "Democracy=5",
              "Democracy=6",
              "Democracy=7",
              "Democracy=8",
              "Democracy=9",
              "Democracy=10",
              "Conflict","Past")
stargazer(res.po1,res.po,
          dep.var.labels=dvnames,
          covariate.labels=varnames,
          omit=c("y>=1","y>=2","y>=3","y>=4","y>=5","y>=6","y>=7","y>=8","Constant","(Intercept)"),
          type="text",omit.stat=c("chi2","f","ser"),font.size="scriptsize",notes.align="l",title="Binary Decomposition of Democracy (Latent Scores by Fariss)",label="total.BinDecompFariss.tex",out="total.BinDecompFariss.tex",no.space=T)











# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 
# Table "Displaying Missing Data"
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 

# re-loading original data here, just in case
load("replicationdata.Rdata") #adjust to your working directory
t.nono <- replicationdata[replicationdata$oecd==0, ] # select developing nations only (non-OECD)
t.nono <- droplevels(t.nono) # drop the unused levels (http://stackoverflow.com/questions/17217951/how-can-i-drop-unused-levels-from-a-data-frame)



# create function to see how much missingness in variables 
propmiss <- function(dataframe) {
  m <- sapply(dataframe, function(x) {
    data.frame(
      nmiss=sum(is.na(x)),
      n=length(x),
      propmiss=sum(is.na(x))/length(x)*100 #note: *100 to display %
    )
  })
  d <- data.frame(t(m))
  d <- sapply(d, unlist)
  d <- as.data.frame(d)
  d$variable <- row.names(d)
  row.names(d) <- NULL
  d <- cbind(d[ncol(d)],d[-ncol(d)])
  return(d[order(d$propmiss), ]) #
} #end of function propmiss; this will not produce any output!



#collect all variables in main models to display missingness
data.sum <- t.nono[,cbind("country","year",
                          "CIRI_PHYSINT",
                          "PTS_ai_reversed",
                          "Fariss_HR",        
                          
                          "Lag_log_UN_FDI_stock_pgdp", 
                          "Lag_lognonnegUS_fdi_totalpGDP",
    
                          "Lag_lognonnegUS_fdi_foodpGDP",
                          "Lag_lognonnegUS_fdi_chemicalpGDP",
                          "Lag_lognonnegUS_fdi_prim_fab_metalpGDP",
                          "Lag_lognonnegUS_fdi_machinerypGDP",
                          "Lag_lognonnegUS_fdi_electricalpGDP",
                          "Lag_lognonnegUS_fdi_transportpGDP",
                          "Lag_lognonnegUS_fdi_whole_tradepGDP",
                          "Lag_lognonnegUS_fdi_depositorypGDP",
                          "Lag_log_Employment_Services", "Lag_log_Employment_Industry",
                          "Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl"
)]
dim(data.sum) #4582 24
data.sum$country <- factor(data.sum$country) #applying factor() to reduce levels of country variable
colnames(data.sum) <- c("Country","Year",
                        
                        "CIRI Phys. Integrity",
                        "PTS Amnesty",
                        "Latent Scores (Fariss)",
                        
                        "Global FDI p. GDP",
                        "U.S. Total FDI p. GDP",
                        
                        "Food FDI p. GDP",
                        "Chemical FDI p. GDP",
                        "Metal FDI p. GDP",
                        "Machinery FDI p. GDP",
                        "Electrical FDI p. GDP",
                        "Transport FDI p. GDP",
                        "Whole. Trade FDI p. GDP",
                        "Deposit. Inst. FDI p. GDP",
                        
                        "Employm. Services",
                        "Employm. Industry",
                        
                        "Trade","GDP p. capita","Population","Democracy","Conflict"                       
) #


b <- propmiss(data.sum) #applying the function we just created above
b#    
#b[,4] <- round(b[,4],2)
b
colnames(b) <- c("Variable","Missing Obs.","Rows","Missingness %")
b <- b[-c(1,2),-3] #remove country, year; "Rows" colum
# display table nicely, can export as .tex file
stargazer(b,summary=F,rownames=F,type="text",title="Displaying Missing Data",font.size="scriptsize",notes=c("Number of total rows: 4,582."),notes.align="l",digits=2,label="miss_data.sum.excl.tex",out="miss_data.sum.excl.tex")








# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 
# Table "Missingness Analysis for Sectoral FDI (Logistic Regression)"
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 


# Dummy Coding Approach (code missing 1 and non-missing 0, then run correlation, chi-square, ologit where outcome being missing vs no missing for each variable and see if any other predictor is associated with the missingness of the variable)
data.sum.excl <- t.nono[,cbind("country","year",
                               "CIRI_PHYSINT",
                               "PTS_ai_reversed",
                               "Fariss_HR",        
                               
                               "Lag_log_UN_FDI_stock_pgdp", 
                               "Lag_lognonnegUS_fdi_totalpGDP",
                               
                               "Lag_lognonnegUS_fdi_foodpGDP",
                               "Lag_lognonnegUS_fdi_chemicalpGDP",
                               "Lag_lognonnegUS_fdi_prim_fab_metalpGDP",
                               "Lag_lognonnegUS_fdi_machinerypGDP",
                               "Lag_lognonnegUS_fdi_electricalpGDP",
                               "Lag_lognonnegUS_fdi_transportpGDP",
                               "Lag_lognonnegUS_fdi_whole_tradepGDP",
                               "Lag_lognonnegUS_fdi_depositorypGDP",
                               "Lag_log_Employment_Services", "Lag_log_Employment_Industry",
                               "Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl"
)]# without pretty column names

# create dummies for each FDI sector 
data.sum.excl$"Lag_lognonnegUS_fdi_foodpGDPdummy" <- 0 # create new variable
data.sum.excl$Lag_lognonnegUS_fdi_foodpGDPdummy[is.na(data.sum.excl$Lag_lognonnegUS_fdi_foodpGDP)] <- 1
data.sum.excl$"Lag_lognonnegUS_fdi_chemicalpGDPdummy" <- 0 # create new variable
data.sum.excl$Lag_lognonnegUS_fdi_chemicalpGDPdummy[is.na(data.sum.excl$Lag_lognonnegUS_fdi_chemicalpGDP)] <- 1
data.sum.excl$"Lag_lognonnegUS_fdi_prim_fab_metalpGDPdummy" <- 0 # create new variable
data.sum.excl$Lag_lognonnegUS_fdi_prim_fab_metalpGDPdummy[is.na(data.sum.excl$Lag_lognonnegUS_fdi_prim_fab_metalpGDP)] <- 1
data.sum.excl$"Lag_lognonnegUS_fdi_machinerypGDPdummy" <- 0 # create new variable
data.sum.excl$Lag_lognonnegUS_fdi_machinerypGDPdummy[is.na(data.sum.excl$Lag_lognonnegUS_fdi_machinerypGDP)] <- 1
data.sum.excl$"Lag_lognonnegUS_fdi_electricalpGDPdummy" <- 0 # create new variable
data.sum.excl$Lag_lognonnegUS_fdi_electricalpGDPdummy[is.na(data.sum.excl$Lag_lognonnegUS_fdi_electricalpGDP)] <- 1
data.sum.excl$"Lag_lognonnegUS_fdi_transportpGDPdummy" <- 0 # create new variable
data.sum.excl$Lag_lognonnegUS_fdi_transportpGDPdummy[is.na(data.sum.excl$Lag_lognonnegUS_fdi_transportpGDP)] <- 1
data.sum.excl$"Lag_lognonnegUS_fdi_whole_tradepGDPdummy" <- 0 # create new variable
data.sum.excl$Lag_lognonnegUS_fdi_whole_tradepGDPdummy[is.na(data.sum.excl$Lag_lognonnegUS_fdi_whole_tradepGDP)] <- 1
data.sum.excl$"Lag_lognonnegUS_fdi_depositorypGDPdummy" <- 0 # create new variable
data.sum.excl$Lag_lognonnegUS_fdi_depositorypGDPdummy[is.na(data.sum.excl$Lag_lognonnegUS_fdi_depositorypGDP)] <- 1

# logit for each sector using the controls 
food <- glm(Lag_lognonnegUS_fdi_foodpGDPdummy ~ Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl, data=data.sum.excl, family=binomial)
chemical <- glm(Lag_lognonnegUS_fdi_chemicalpGDPdummy ~ Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl, data=data.sum.excl, family=binomial)
prim_fab_metal <- glm(Lag_lognonnegUS_fdi_prim_fab_metalpGDPdummy ~ Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl, data=data.sum.excl, family=binomial)
machinery <- glm(Lag_lognonnegUS_fdi_machinerypGDPdummy ~ Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl, data=data.sum.excl, family=binomial)
electrical <- glm(Lag_lognonnegUS_fdi_electricalpGDPdummy ~ Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl, data=data.sum.excl, family=binomial)
transport <- glm(Lag_lognonnegUS_fdi_transportpGDPdummy ~ Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl, data=data.sum.excl, family=binomial)
whole_trade <- glm(Lag_lognonnegUS_fdi_whole_tradepGDPdummy ~ Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl, data=data.sum.excl, family=binomial)
depository <- glm(Lag_lognonnegUS_fdi_depositorypGDPdummy ~ Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl, data=data.sum.excl, family=binomial)

# Table "Missingness Analysis for Sectoral FDI"
varnames <- c("Trade", "GDP pc", "Population", "Democracy", "Conflict")
dvnames <- c("Food","Chemical","Metals","Machin.","Electr.","Transp.","Wholes. Tr.","Deposit.")
stargazer(food,chemical,prim_fab_metal,machinery,electrical,transport,whole_trade,depository,
          dep.var.labels=dvnames,
          covariate.labels=varnames,
          omit=c("y>=1","y>=2","y>=3","y>=4","y>=5","y>=6","y>=7","y>=8","factor","Constant","(Intercept)"),
          type="text",omit.stat=c("chi2","f","ser","ll","aic","n"),
          notes=c("Dependent variables: Dummies indicate when original sectoral FDI is missing (= 1)","GDPpc, population, trade are logged. Number of obs.: 3,005."),font.size="scriptsize",notes.align="l",title="Missingness Analysis for Sectoral FDI (Logistic Regression)",no.space=T,label="miss_diagn_dummy.tex",out="miss_diagn_dummy.tex")










#####################################################
# Tables:
# "Sectoral FDI and CIRI Physical Integrity Rights (sectors entered separately)"
# "Sectoral FDI and PTS Amnesty Scores (sectors entered separately)"
# "Sectoral FDI and Fariss Latent Scores (sectors entered separately)"
#####################################################


# # Sectoral (exclusive resource) FDI ENTERED SEPARATELY ####
data.CIRI.PHY.excl.LDVfood <- na.omit(t.nono[,cbind("CIRI_PHYSINT","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_foodpGDP","Lag_CIRI_PHYSINT", "country","year")])
data.CIRI.PHY.excl.LDVchem <- na.omit(t.nono[,cbind("CIRI_PHYSINT","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_chemicalpGDP","Lag_CIRI_PHYSINT", "country","year")])
data.CIRI.PHY.excl.LDVmetal <- na.omit(t.nono[,cbind("CIRI_PHYSINT","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_prim_fab_metalpGDP","Lag_CIRI_PHYSINT", "country","year")])
data.CIRI.PHY.excl.LDVmachin <- na.omit(t.nono[,cbind("CIRI_PHYSINT","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_machinerypGDP","Lag_CIRI_PHYSINT", "country","year")])
data.CIRI.PHY.excl.LDVelect <- na.omit(t.nono[,cbind("CIRI_PHYSINT","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_electricalpGDP","Lag_CIRI_PHYSINT", "country","year")])
data.CIRI.PHY.excl.LDVtransp <- na.omit(t.nono[,cbind("CIRI_PHYSINT","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_transportpGDP","Lag_CIRI_PHYSINT", "country","year")])
data.CIRI.PHY.excl.LDVwhotra <- na.omit(t.nono[,cbind("CIRI_PHYSINT","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_whole_tradepGDP","Lag_CIRI_PHYSINT", "country","year")])
data.CIRI.PHY.excl.LDVdeposi <- na.omit(t.nono[,cbind("CIRI_PHYSINT","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_depositorypGDP","Lag_CIRI_PHYSINT", "country","year")])
#add petrol and mining
data.CIRI.PHY.excl.LDVpetrol <- na.omit(t.nono[,cbind("CIRI_PHYSINT","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_petrolpGDP","Lag_CIRI_PHYSINT", "country","year")])
data.CIRI.PHY.excl.LDVmining <- na.omit(t.nono[,cbind("CIRI_PHYSINT","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_miningpGDP","Lag_CIRI_PHYSINT", "country","year")])
data.CIRI.PHY.excl.LDV <- na.omit(t.nono[,cbind("CIRI_PHYSINT","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_foodpGDP","Lag_lognonnegUS_fdi_chemicalpGDP","Lag_lognonnegUS_fdi_prim_fab_metalpGDP","Lag_lognonnegUS_fdi_machinerypGDP","Lag_lognonnegUS_fdi_electricalpGDP","Lag_lognonnegUS_fdi_transportpGDP","Lag_lognonnegUS_fdi_whole_tradepGDP","Lag_lognonnegUS_fdi_depositorypGDP","Lag_CIRI_PHYSINT", "country","year")])


data.PTS.excl.LDVfood <- na.omit(t.nono[,cbind("PTS_ai_reversed","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_foodpGDP","Lag_PTS_ai_reversed", "country","year")])
data.PTS.excl.LDVchem <- na.omit(t.nono[,cbind("PTS_ai_reversed","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_chemicalpGDP","Lag_PTS_ai_reversed", "country","year")])
data.PTS.excl.LDVmetal <- na.omit(t.nono[,cbind("PTS_ai_reversed","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_prim_fab_metalpGDP","Lag_PTS_ai_reversed", "country","year")])
data.PTS.excl.LDVmachin <- na.omit(t.nono[,cbind("PTS_ai_reversed","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_machinerypGDP","Lag_PTS_ai_reversed", "country","year")])
data.PTS.excl.LDVelect <- na.omit(t.nono[,cbind("PTS_ai_reversed","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_electricalpGDP","Lag_PTS_ai_reversed", "country","year")])
data.PTS.excl.LDVtransp <- na.omit(t.nono[,cbind("PTS_ai_reversed","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_transportpGDP","Lag_PTS_ai_reversed", "country","year")])
data.PTS.excl.LDVwhotra <- na.omit(t.nono[,cbind("PTS_ai_reversed","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_whole_tradepGDP","Lag_PTS_ai_reversed", "country","year")])
data.PTS.excl.LDVdeposi <- na.omit(t.nono[,cbind("PTS_ai_reversed","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_depositorypGDP","Lag_PTS_ai_reversed", "country","year")])
#add petrol and mining
data.PTS.excl.LDVpetrol <- na.omit(t.nono[,cbind("PTS_ai_reversed","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_petrolpGDP","Lag_PTS_ai_reversed", "country","year")])
data.PTS.excl.LDVmining <- na.omit(t.nono[,cbind("PTS_ai_reversed","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_miningpGDP","Lag_PTS_ai_reversed", "country","year")])
data.PTS.excl.LDV  <- na.omit(t.nono[,cbind("PTS_ai_reversed","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_foodpGDP","Lag_lognonnegUS_fdi_chemicalpGDP","Lag_lognonnegUS_fdi_prim_fab_metalpGDP","Lag_lognonnegUS_fdi_machinerypGDP","Lag_lognonnegUS_fdi_electricalpGDP","Lag_lognonnegUS_fdi_transportpGDP","Lag_lognonnegUS_fdi_whole_tradepGDP","Lag_lognonnegUS_fdi_depositorypGDP","Lag_PTS_ai_reversed","country","year")])

data.Fariss.excl.LDVfood <- na.omit(t.nono[,cbind("Fariss_HR","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_foodpGDP","Lag_Fariss_HR", "country","year")])
data.Fariss.excl.LDVfood <- as.data.frame(data.Fariss.excl.LDVfood)

data.Fariss.excl.LDVchem <- na.omit(t.nono[,cbind("Fariss_HR","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_chemicalpGDP","Lag_Fariss_HR", "country","year")])
data.Fariss.excl.LDVchem <- as.data.frame(data.Fariss.excl.LDVchem)

data.Fariss.excl.LDVmetal <- na.omit(t.nono[,cbind("Fariss_HR","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_prim_fab_metalpGDP","Lag_Fariss_HR", "country","year")])
data.Fariss.excl.LDVmetal <- as.data.frame(data.Fariss.excl.LDVmetal)

data.Fariss.excl.LDVmachin <- na.omit(t.nono[,cbind("Fariss_HR","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_machinerypGDP","Lag_Fariss_HR", "country","year")])
data.Fariss.excl.LDVmachin <- as.data.frame(data.Fariss.excl.LDVmachin)

data.Fariss.excl.LDVelect <- na.omit(t.nono[,cbind("Fariss_HR","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_electricalpGDP","Lag_Fariss_HR", "country","year")])
data.Fariss.excl.LDVelect <- as.data.frame(data.Fariss.excl.LDVelect)

data.Fariss.excl.LDVtransp <- na.omit(t.nono[,cbind("Fariss_HR","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_transportpGDP","Lag_Fariss_HR", "country","year")])
data.Fariss.excl.LDVtransp <- as.data.frame(data.Fariss.excl.LDVtransp)

data.Fariss.excl.LDVwhotra <- na.omit(t.nono[,cbind("Fariss_HR","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_whole_tradepGDP","Lag_Fariss_HR", "country","year")])
data.Fariss.excl.LDVwhotra <- as.data.frame(data.Fariss.excl.LDVwhotra)

data.Fariss.excl.LDVdeposi <- na.omit(t.nono[,cbind("Fariss_HR","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_depositorypGDP","Lag_Fariss_HR", "country","year")])
data.Fariss.excl.LDVdeposi <- as.data.frame(data.Fariss.excl.LDVdeposi)

#add petrol and mining
data.Fariss.excl.LDVpetrol <- na.omit(t.nono[,cbind("Fariss_HR","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_petrolpGDP","Lag_Fariss_HR", "country","year")])
data.Fariss.excl.LDVpetrol <- as.data.frame(data.Fariss.excl.LDVpetrol)

data.Fariss.excl.LDVmining <- na.omit(t.nono[,cbind("Fariss_HR","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_miningpGDP","Lag_Fariss_HR", "country","year")])
data.Fariss.excl.LDVmining <- as.data.frame(data.Fariss.excl.LDVmining)

data.Fariss.excl.LDV <- na.omit(t.nono[,cbind("Fariss_HR","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_foodpGDP","Lag_lognonnegUS_fdi_chemicalpGDP","Lag_lognonnegUS_fdi_prim_fab_metalpGDP","Lag_lognonnegUS_fdi_machinerypGDP","Lag_lognonnegUS_fdi_electricalpGDP","Lag_lognonnegUS_fdi_transportpGDP","Lag_lognonnegUS_fdi_whole_tradepGDP","Lag_lognonnegUS_fdi_depositorypGDP","Lag_Fariss_HR","country","year")])
data.Fariss.excl.LDV <- as.data.frame(data.Fariss.excl.LDV)


# MODELS ####
# Models CIRI and all sectors separately
CIRI.logit.LDV.exclfood <- lrm(CIRI_PHYSINT ~ Lag_lognonnegUS_fdi_foodpGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_CIRI_PHYSINT,data=data.CIRI.PHY.excl.LDVfood,x=TRUE, y=TRUE)
CIRI.logit.LDV.excl.corrfood <- robcov(CIRI.logit.LDV.exclfood,data.CIRI.PHY.excl.LDVfood$country) #robust errors  

CIRI.logit.LDV.exclchem <- lrm(CIRI_PHYSINT ~ Lag_lognonnegUS_fdi_chemicalpGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_CIRI_PHYSINT,data=data.CIRI.PHY.excl.LDVchem,x=TRUE, y=TRUE)
CIRI.logit.LDV.excl.corrchem <- robcov(CIRI.logit.LDV.exclchem,data.CIRI.PHY.excl.LDVchem$country) #robust errors  

CIRI.logit.LDV.exclmetal <- lrm(CIRI_PHYSINT ~ Lag_lognonnegUS_fdi_prim_fab_metalpGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_CIRI_PHYSINT,data=data.CIRI.PHY.excl.LDVmetal,x=TRUE, y=TRUE)
CIRI.logit.LDV.excl.corrmetal <- robcov(CIRI.logit.LDV.exclmetal,data.CIRI.PHY.excl.LDVmetal$country) #robust errors  

CIRI.logit.LDV.exclmachin <- lrm(CIRI_PHYSINT ~ Lag_lognonnegUS_fdi_machinerypGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_CIRI_PHYSINT,data=data.CIRI.PHY.excl.LDVmachin,x=TRUE, y=TRUE)
CIRI.logit.LDV.excl.corrmachin <- robcov(CIRI.logit.LDV.exclmachin,data.CIRI.PHY.excl.LDVmachin$country) #robust errors  

CIRI.logit.LDV.exclelect <- lrm(CIRI_PHYSINT ~ Lag_lognonnegUS_fdi_electricalpGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_CIRI_PHYSINT,data=data.CIRI.PHY.excl.LDVelect,x=TRUE, y=TRUE)
CIRI.logit.LDV.excl.correlect <- robcov(CIRI.logit.LDV.exclelect,data.CIRI.PHY.excl.LDVelect$country) #robust errors  

CIRI.logit.LDV.excltransp <- lrm(CIRI_PHYSINT ~ Lag_lognonnegUS_fdi_transportpGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_CIRI_PHYSINT,data=data.CIRI.PHY.excl.LDVtransp,x=TRUE, y=TRUE)
CIRI.logit.LDV.excl.corrtransp <- robcov(CIRI.logit.LDV.excltransp,data.CIRI.PHY.excl.LDVtransp$country) #robust errors  

CIRI.logit.LDV.exclwhotra <- lrm(CIRI_PHYSINT ~ Lag_lognonnegUS_fdi_whole_tradepGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_CIRI_PHYSINT,data=data.CIRI.PHY.excl.LDVwhotra,x=TRUE, y=TRUE)
CIRI.logit.LDV.excl.corrwhotra <- robcov(CIRI.logit.LDV.exclwhotra,data.CIRI.PHY.excl.LDVwhotra$country) #robust errors  

CIRI.logit.LDV.excldeposi <- lrm(CIRI_PHYSINT ~ Lag_lognonnegUS_fdi_depositorypGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_CIRI_PHYSINT,data=data.CIRI.PHY.excl.LDVdeposi,x=TRUE, y=TRUE)
CIRI.logit.LDV.excl.corrdeposi <- robcov(CIRI.logit.LDV.excldeposi,data.CIRI.PHY.excl.LDVdeposi$country) #robust errors  

CIRI.logit.LDV.exclpetrol <- lrm(CIRI_PHYSINT ~ Lag_lognonnegUS_fdi_petrolpGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_CIRI_PHYSINT,data=data.CIRI.PHY.excl.LDVpetrol,x=TRUE, y=TRUE)
CIRI.logit.LDV.excl.corrpetrol <- robcov(CIRI.logit.LDV.exclpetrol,data.CIRI.PHY.excl.LDVpetrol$country) #robust errors  

CIRI.logit.LDV.exclmining<- lrm(CIRI_PHYSINT ~ Lag_lognonnegUS_fdi_miningpGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_CIRI_PHYSINT,data=data.CIRI.PHY.excl.LDVmining,x=TRUE, y=TRUE)
CIRI.logit.LDV.excl.corrmining <- robcov(CIRI.logit.LDV.exclmining,data.CIRI.PHY.excl.LDVmining$country) #robust errors  

CIRI.logit.LDV.excl <- lrm(CIRI_PHYSINT ~ Lag_lognonnegUS_fdi_foodpGDP + Lag_lognonnegUS_fdi_chemicalpGDP + Lag_lognonnegUS_fdi_prim_fab_metalpGDP + Lag_lognonnegUS_fdi_machinerypGDP + Lag_lognonnegUS_fdi_electricalpGDP + Lag_lognonnegUS_fdi_transportpGDP + Lag_lognonnegUS_fdi_whole_tradepGDP + Lag_lognonnegUS_fdi_depositorypGDP+ Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_CIRI_PHYSINT,data=data.CIRI.PHY.excl.LDV  ,x=TRUE, y=TRUE)
CIRI.logit.LDV.excl.corr <- robcov(CIRI.logit.LDV.excl,data.CIRI.PHY.excl.LDV$country) #robust errors  


# Models PTS and all sectors separately
PTS.logit.LDV.exclfood <- lrm(PTS_ai_reversed ~ Lag_lognonnegUS_fdi_foodpGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_PTS_ai_reversed,data=data.PTS.excl.LDVfood,x=TRUE, y=TRUE)
PTS.logit.LDV.excl.corrfood <- robcov(PTS.logit.LDV.exclfood,data.PTS.excl.LDVfood$country) #robust errors  

PTS.logit.LDV.exclchem <- lrm(PTS_ai_reversed ~ Lag_lognonnegUS_fdi_chemicalpGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_PTS_ai_reversed,data=data.PTS.excl.LDVchem,x=TRUE, y=TRUE)
PTS.logit.LDV.excl.corrchem <- robcov(PTS.logit.LDV.exclchem,data.PTS.excl.LDVchem$country) #robust errors  

PTS.logit.LDV.exclmetal <- lrm(PTS_ai_reversed ~ Lag_lognonnegUS_fdi_prim_fab_metalpGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_PTS_ai_reversed,data=data.PTS.excl.LDVmetal,x=TRUE, y=TRUE)
PTS.logit.LDV.excl.corrmetal <- robcov(PTS.logit.LDV.exclmetal,data.PTS.excl.LDVmetal$country) #robust errors  

PTS.logit.LDV.exclmachin <- lrm(PTS_ai_reversed ~ Lag_lognonnegUS_fdi_machinerypGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_PTS_ai_reversed,data=data.PTS.excl.LDVmachin,x=TRUE, y=TRUE)
PTS.logit.LDV.excl.corrmachin <- robcov(PTS.logit.LDV.exclmachin,data.PTS.excl.LDVmachin$country) #robust errors  

PTS.logit.LDV.exclelect <- lrm(PTS_ai_reversed ~ Lag_lognonnegUS_fdi_electricalpGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_PTS_ai_reversed,data=data.PTS.excl.LDVelect,x=TRUE, y=TRUE)
PTS.logit.LDV.excl.correlect <- robcov(PTS.logit.LDV.exclelect,data.PTS.excl.LDVelect$country) #robust errors  

PTS.logit.LDV.excltransp <- lrm(PTS_ai_reversed ~ Lag_lognonnegUS_fdi_transportpGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_PTS_ai_reversed,data=data.PTS.excl.LDVtransp,x=TRUE, y=TRUE)
PTS.logit.LDV.excl.corrtransp <- robcov(PTS.logit.LDV.excltransp,data.PTS.excl.LDVtransp$country) #robust errors  

PTS.logit.LDV.exclwhotra <- lrm(PTS_ai_reversed ~ Lag_lognonnegUS_fdi_whole_tradepGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_PTS_ai_reversed,data=data.PTS.excl.LDVwhotra,x=TRUE, y=TRUE)
PTS.logit.LDV.excl.corrwhotra <- robcov(PTS.logit.LDV.exclwhotra,data.PTS.excl.LDVwhotra$country) #robust errors  

PTS.logit.LDV.excldeposi <- lrm(PTS_ai_reversed ~ Lag_lognonnegUS_fdi_depositorypGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_PTS_ai_reversed,data=data.PTS.excl.LDVdeposi,x=TRUE, y=TRUE)
PTS.logit.LDV.excl.corrdeposi <- robcov(PTS.logit.LDV.excldeposi,data.PTS.excl.LDVdeposi$country) #robust errors  

PTS.logit.LDV.exclpetrol <- lrm(PTS_ai_reversed ~ Lag_lognonnegUS_fdi_petrolpGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_PTS_ai_reversed,data=data.PTS.excl.LDVpetrol,x=TRUE, y=TRUE)
PTS.logit.LDV.excl.corrpetrol <- robcov(PTS.logit.LDV.exclpetrol,data.PTS.excl.LDVpetrol$country) #robust errors  

PTS.logit.LDV.exclmining<- lrm(PTS_ai_reversed ~ Lag_lognonnegUS_fdi_miningpGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_PTS_ai_reversed,data=data.PTS.excl.LDVmining,x=TRUE, y=TRUE)
PTS.logit.LDV.excl.corrmining <- robcov(PTS.logit.LDV.exclmining,data.PTS.excl.LDVmining$country) #robust errors  

PTS.logit.LDV.excl <- lrm(PTS_ai_reversed ~ Lag_lognonnegUS_fdi_foodpGDP + Lag_lognonnegUS_fdi_chemicalpGDP + Lag_lognonnegUS_fdi_prim_fab_metalpGDP + Lag_lognonnegUS_fdi_machinerypGDP + Lag_lognonnegUS_fdi_electricalpGDP + Lag_lognonnegUS_fdi_transportpGDP + Lag_lognonnegUS_fdi_whole_tradepGDP + Lag_lognonnegUS_fdi_depositorypGDP+ Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_PTS_ai_reversed,data=data.PTS.excl.LDV,x=TRUE, y=TRUE)
PTS.logit.LDV.excl.corr <- robcov(PTS.logit.LDV.excl,data.PTS.excl.LDV$country) 


# Fariss Models each FDI separately
form   <- Fariss_HR ~ Lag_lognonnegUS_fdi_foodpGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_Fariss_HR
res.po.exclfood <- plm(form,model="pooling",index=c("country","year"),data=data.Fariss.excl.LDVfood)
## PCSE a la Beck-Katz
res.BKtime.exclfood  <- vcovBK(x=res.po.exclfood,cluster="time")
ct.time.exclfood     <- coeftest(res.po.exclfood,vcov=res.BKtime.exclfood)
res.po.exclfood$vcov <- res.BKtime.exclfood  # replacing the first uncorrected residual covariance structure with the corrected SE
print(ct.time.exclfood)

form   <- Fariss_HR ~ Lag_lognonnegUS_fdi_chemicalpGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_Fariss_HR
res.po.exclchem <- plm(form,model="pooling",index=c("country","year"),data=data.Fariss.excl.LDVchem)
## PCSE a la Beck-Katz
res.BKtime.exclchem  <- vcovBK(x=res.po.exclchem,cluster="time")
ct.time.exclchem    <- coeftest(res.po.exclchem ,vcov=res.BKtime.exclchem )
res.po.exclchem$vcov <- res.BKtime.exclchem   # replacing the first uncorrected residual covariance structure with the corrected SE
print(ct.time.exclchem)

form   <- Fariss_HR ~  Lag_lognonnegUS_fdi_prim_fab_metalpGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_Fariss_HR
res.po.exclmetal <- plm(form,model="pooling",index=c("country","year"),data=data.Fariss.excl.LDVmetal)
## PCSE a la Beck-Katz
res.BKtime.exclmetal  <- vcovBK(x=res.po.exclmetal,cluster="time")
ct.time.exclmetal    <- coeftest(res.po.exclmetal ,vcov=res.BKtime.exclmetal)
res.po.exclmetal$vcov <- res.BKtime.exclmetal   # replacing the first uncorrected residual covariance structure with the corrected SE
print(ct.time.exclmetal)

form   <- Fariss_HR ~  Lag_lognonnegUS_fdi_machinerypGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_Fariss_HR
res.po.exclmachin <- plm(form,model="pooling",index=c("country","year"),data=data.Fariss.excl.LDVmachin)
## PCSE a la Beck-Katz
res.BKtime.exclmachin  <- vcovBK(x=res.po.exclmachin,cluster="time")
ct.time.exclmachin    <- coeftest(res.po.exclmachin ,vcov=res.BKtime.exclmachin)
res.po.exclmachin$vcov <- res.BKtime.exclmachin   # replacing the first uncorrected residual covariance structure with the corrected SE
print(ct.time.exclmachin)

form   <- Fariss_HR ~  Lag_lognonnegUS_fdi_electricalpGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_Fariss_HR
res.po.exclelect <- plm(form,model="pooling",index=c("country","year"),data=data.Fariss.excl.LDVelect)
## PCSE a la Beck-Katz
res.BKtime.exclelect  <- vcovBK(x=res.po.exclelect,cluster="time")
ct.time.exclelect    <- coeftest(res.po.exclelect ,vcov=res.BKtime.exclelect)
res.po.exclelect$vcov <- res.BKtime.exclelect   # replacing the first uncorrected residual covariance structure with the corrected SE
print(ct.time.exclelect)

form   <- Fariss_HR ~  Lag_lognonnegUS_fdi_transportpGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_Fariss_HR
res.po.excltransp <- plm(form,model="pooling",index=c("country","year"),data=data.Fariss.excl.LDVtransp)
## PCSE a la Beck-Katz
res.BKtime.excltransp  <- vcovBK(x=res.po.excltransp,cluster="time")
ct.time.excltransp    <- coeftest(res.po.excltransp ,vcov=res.BKtime.excltransp)
res.po.excltransp$vcov <- res.BKtime.excltransp   # replacing the first uncorrected residual covariance structure with the corrected SE
print(ct.time.excltransp)

form   <- Fariss_HR ~  Lag_lognonnegUS_fdi_whole_tradepGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_Fariss_HR
res.po.exclwhotra <- plm(form,model="pooling",index=c("country","year"),data=data.Fariss.excl.LDVwhotra)
## PCSE a la Beck-Katz
res.BKtime.exclwhotra <- vcovBK(x=res.po.exclwhotra,cluster="time")
ct.time.exclwhotra   <- coeftest(res.po.exclwhotra,vcov=res.BKtime.exclwhotra)
res.po.exclwhotra$vcov <- res.BKtime.exclwhotra   # replacing the first uncorrected residual covariance structure with the corrected SE
print(ct.time.exclwhotra)

form   <- Fariss_HR ~  Lag_lognonnegUS_fdi_depositorypGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_Fariss_HR
res.po.excldeposi <- plm(form,model="pooling",index=c("country","year"),data=data.Fariss.excl.LDVdeposi)
## PCSE a la Beck-Katz
res.BKtime.excldeposi <- vcovBK(x=res.po.excldeposi,cluster="time")
ct.time.excldeposi   <- coeftest(res.po.excldeposi,vcov=res.BKtime.excldeposi)
res.po.excldeposi$vcov <- res.BKtime.excldeposi   # replacing the first uncorrected residual covariance structure with the corrected SE
print(ct.time.excldeposi)

form   <- Fariss_HR ~  Lag_lognonnegUS_fdi_petrolpGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_Fariss_HR
res.po.exclpetrol <- plm(form,model="pooling",index=c("country","year"),data=data.Fariss.excl.LDVpetrol)
## PCSE a la Beck-Katz
res.BKtime.exclpetrol <- vcovBK(x=res.po.exclpetrol,cluster="time")
ct.time.exclpetrol   <- coeftest(res.po.exclpetrol,vcov=res.BKtime.exclpetrol)
res.po.exclpetrol$vcov <- res.BKtime.exclpetrol   # replacing the first uncorrected residual covariance structure with the corrected SE
print(ct.time.exclpetrol)

form   <- Fariss_HR ~  Lag_lognonnegUS_fdi_miningpGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_Fariss_HR
res.po.exclmining <- plm(form,model="pooling",index=c("country","year"),data=data.Fariss.excl.LDVmining)
## PCSE a la Beck-Katz
res.BKtime.exclmining <- vcovBK(x=res.po.exclmining,cluster="time")
ct.time.exclmining   <- coeftest(res.po.exclmining,vcov=res.BKtime.exclmining)
res.po.exclmining$vcov <- res.BKtime.exclmining   # replacing the first uncorrected residual covariance structure with the corrected SE
print(ct.time.exclmining)

form   <- Fariss_HR ~  Lag_lognonnegUS_fdi_foodpGDP + Lag_lognonnegUS_fdi_chemicalpGDP + Lag_lognonnegUS_fdi_prim_fab_metalpGDP + Lag_lognonnegUS_fdi_machinerypGDP + Lag_lognonnegUS_fdi_electricalpGDP + Lag_lognonnegUS_fdi_transportpGDP + Lag_lognonnegUS_fdi_whole_tradepGDP + Lag_lognonnegUS_fdi_depositorypGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_Fariss_HR
res.po.excl <- plm(form,model="pooling",index=c("country","year"),data=data.Fariss.excl.LDV)
## PCSE a la Beck-Katz
res.BKtime.excl  <- vcovBK(x=res.po.excl,cluster="time")
ct.time.excl     <- coeftest(res.po.excl,vcov=res.BKtime.excl)
res.po.excl$vcov <- res.BKtime.excl  # replacing the first uncorrected residual covariance structure with the corrected SE
print(ct.time.excl)



# Create three tables (CIRI, PTS, Fariss) with these results, last column is what T5 used to be (all sectors in one exclusive petrol and mining)

#?rename lagged dependent variable in R output objects so they will go in the same row called "Past" in results table
names(CIRI.logit.LDV.excl.corrfood$coefficients)[names(CIRI.logit.LDV.excl.corrfood$coefficients) == "Lag_CIRI_PHYSINT"] <- "Past"
rownames(CIRI.logit.LDV.excl.corrfood$var)[rownames(CIRI.logit.LDV.excl.corrfood$var)== "Lag_CIRI_PHYSINT"] <- "Past"
colnames(CIRI.logit.LDV.excl.corrfood$var)[colnames(CIRI.logit.LDV.excl.corrfood$var)== "Lag_CIRI_PHYSINT"] <- "Past"

names(CIRI.logit.LDV.excl.corrchem$coefficients)[names(CIRI.logit.LDV.excl.corrchem$coefficients) == "Lag_CIRI_PHYSINT"] <- "Past"
rownames(CIRI.logit.LDV.excl.corrchem$var)[rownames(CIRI.logit.LDV.excl.corrchem$var)== "Lag_CIRI_PHYSINT"] <- "Past"
colnames(CIRI.logit.LDV.excl.corrchem$var)[colnames(CIRI.logit.LDV.excl.corrchem$var)== "Lag_CIRI_PHYSINT"] <- "Past"

names(CIRI.logit.LDV.excl.corrmetal$coefficients)[names(CIRI.logit.LDV.excl.corrmetal$coefficients) == "Lag_CIRI_PHYSINT"] <- "Past"
rownames(CIRI.logit.LDV.excl.corrmetal$var)[rownames(CIRI.logit.LDV.excl.corrmetal$var)== "Lag_CIRI_PHYSINT"] <- "Past"
colnames(CIRI.logit.LDV.excl.corrmetal$var)[colnames(CIRI.logit.LDV.excl.corrmetal$var)== "Lag_CIRI_PHYSINT"] <- "Past"

names(CIRI.logit.LDV.excl.corrmachin$coefficients)[names(CIRI.logit.LDV.excl.corrmachin$coefficients) == "Lag_CIRI_PHYSINT"] <- "Past"
rownames(CIRI.logit.LDV.excl.corrmachin$var)[rownames(CIRI.logit.LDV.excl.corrmachin$var)== "Lag_CIRI_PHYSINT"] <- "Past"
colnames(CIRI.logit.LDV.excl.corrmachin$var)[colnames(CIRI.logit.LDV.excl.corrmachin$var)== "Lag_CIRI_PHYSINT"] <- "Past"

names(CIRI.logit.LDV.excl.correlect$coefficients)[names(CIRI.logit.LDV.excl.correlect$coefficients) == "Lag_CIRI_PHYSINT"] <- "Past"
rownames(CIRI.logit.LDV.excl.correlect$var)[rownames(CIRI.logit.LDV.excl.correlect$var)== "Lag_CIRI_PHYSINT"] <- "Past"
colnames(CIRI.logit.LDV.excl.correlect$var)[colnames(CIRI.logit.LDV.excl.correlect$var)== "Lag_CIRI_PHYSINT"] <- "Past"

names(CIRI.logit.LDV.excl.corrtransp$coefficients)[names(CIRI.logit.LDV.excl.corrtransp$coefficients) == "Lag_CIRI_PHYSINT"] <- "Past"
rownames(CIRI.logit.LDV.excl.corrtransp$var)[rownames(CIRI.logit.LDV.excl.corrtransp$var)== "Lag_CIRI_PHYSINT"] <- "Past"
colnames(CIRI.logit.LDV.excl.corrtransp$var)[colnames(CIRI.logit.LDV.excl.corrtransp$var)== "Lag_CIRI_PHYSINT"] <- "Past"

names(CIRI.logit.LDV.excl.corrwhotra$coefficients)[names(CIRI.logit.LDV.excl.corrwhotra$coefficients) == "Lag_CIRI_PHYSINT"] <- "Past"
rownames(CIRI.logit.LDV.excl.corrwhotra$var)[rownames(CIRI.logit.LDV.excl.corrwhotra$var)== "Lag_CIRI_PHYSINT"] <- "Past"
colnames(CIRI.logit.LDV.excl.corrwhotra$var)[colnames(CIRI.logit.LDV.excl.corrwhotra$var)== "Lag_CIRI_PHYSINT"] <- "Past"

names(CIRI.logit.LDV.excl.corrdeposi$coefficients)[names(CIRI.logit.LDV.excl.corrdeposi$coefficients) == "Lag_CIRI_PHYSINT"] <- "Past"
rownames(CIRI.logit.LDV.excl.corrdeposi$var)[rownames(CIRI.logit.LDV.excl.corrdeposi$var)== "Lag_CIRI_PHYSINT"] <- "Past"
colnames(CIRI.logit.LDV.excl.corrdeposi$var)[colnames(CIRI.logit.LDV.excl.corrdeposi$var)== "Lag_CIRI_PHYSINT"] <- "Past"

names(CIRI.logit.LDV.excl.corrpetrol$coefficients)[names(CIRI.logit.LDV.excl.corrpetrol$coefficients) == "Lag_CIRI_PHYSINT"] <- "Past"
rownames(CIRI.logit.LDV.excl.corrpetrol$var)[rownames(CIRI.logit.LDV.excl.corrpetrol$var)== "Lag_CIRI_PHYSINT"] <- "Past"
colnames(CIRI.logit.LDV.excl.corrpetrol$var)[colnames(CIRI.logit.LDV.excl.corrpetrol$var)== "Lag_CIRI_PHYSINT"] <- "Past"

names(CIRI.logit.LDV.excl.corrmining$coefficients)[names(CIRI.logit.LDV.excl.corrmining$coefficients) == "Lag_CIRI_PHYSINT"] <- "Past"
rownames(CIRI.logit.LDV.excl.corrmining$var)[rownames(CIRI.logit.LDV.excl.corrmining$var)== "Lag_CIRI_PHYSINT"] <- "Past"
colnames(CIRI.logit.LDV.excl.corrmining$var)[colnames(CIRI.logit.LDV.excl.corrmining$var)== "Lag_CIRI_PHYSINT"] <- "Past"

names(CIRI.logit.LDV.excl.corr$coefficients)[names(CIRI.logit.LDV.excl.corr$coefficients) == "Lag_CIRI_PHYSINT"] <- "Past"
rownames(CIRI.logit.LDV.excl.corr$var)[rownames(CIRI.logit.LDV.excl.corr$var)== "Lag_CIRI_PHYSINT"] <- "Past"
colnames(CIRI.logit.LDV.excl.corr$var)[colnames(CIRI.logit.LDV.excl.corr$var)== "Lag_CIRI_PHYSINT"] <- "Past"




# pts renaming
names(PTS.logit.LDV.excl.corrfood$coefficients)[names(PTS.logit.LDV.excl.corrfood$coefficients) == "Lag_PTS_ai_reversed"] <- "Past"
rownames(PTS.logit.LDV.excl.corrfood$var)[rownames(PTS.logit.LDV.excl.corrfood$var)== "Lag_PTS_ai_reversed"] <- "Past"
colnames(PTS.logit.LDV.excl.corrfood$var)[colnames(PTS.logit.LDV.excl.corrfood$var)== "Lag_PTS_ai_reversed"] <- "Past"

names(PTS.logit.LDV.excl.corrchem$coefficients)[names(PTS.logit.LDV.excl.corrchem$coefficients) == "Lag_PTS_ai_reversed"] <- "Past"
rownames(PTS.logit.LDV.excl.corrchem$var)[rownames(PTS.logit.LDV.excl.corrchem$var)== "Lag_PTS_ai_reversed"] <- "Past"
colnames(PTS.logit.LDV.excl.corrchem$var)[colnames(PTS.logit.LDV.excl.corrchem$var)== "Lag_PTS_ai_reversed"] <- "Past"

names(PTS.logit.LDV.excl.corrmetal$coefficients)[names(PTS.logit.LDV.excl.corrmetal$coefficients) == "Lag_PTS_ai_reversed"] <- "Past"
rownames(PTS.logit.LDV.excl.corrmetal$var)[rownames(PTS.logit.LDV.excl.corrmetal$var)== "Lag_PTS_ai_reversed"] <- "Past"
colnames(PTS.logit.LDV.excl.corrmetal$var)[colnames(PTS.logit.LDV.excl.corrmetal$var)== "Lag_PTS_ai_reversed"] <- "Past"

names(PTS.logit.LDV.excl.corrmachin$coefficients)[names(PTS.logit.LDV.excl.corrmachin$coefficients) == "Lag_PTS_ai_reversed"] <- "Past"
rownames(PTS.logit.LDV.excl.corrmachin$var)[rownames(PTS.logit.LDV.excl.corrmachin$var)== "Lag_PTS_ai_reversed"] <- "Past"
colnames(PTS.logit.LDV.excl.corrmachin$var)[colnames(PTS.logit.LDV.excl.corrmachin$var)== "Lag_PTS_ai_reversed"] <- "Past"

names(PTS.logit.LDV.excl.correlect$coefficients)[names(PTS.logit.LDV.excl.correlect$coefficients) == "Lag_PTS_ai_reversed"] <- "Past"
rownames(PTS.logit.LDV.excl.correlect$var)[rownames(PTS.logit.LDV.excl.correlect$var)== "Lag_PTS_ai_reversed"] <- "Past"
colnames(PTS.logit.LDV.excl.correlect$var)[colnames(PTS.logit.LDV.excl.correlect$var)== "Lag_PTS_ai_reversed"] <- "Past"

names(PTS.logit.LDV.excl.corrtransp$coefficients)[names(PTS.logit.LDV.excl.corrtransp$coefficients) == "Lag_PTS_ai_reversed"] <- "Past"
rownames(PTS.logit.LDV.excl.corrtransp$var)[rownames(PTS.logit.LDV.excl.corrtransp$var)== "Lag_PTS_ai_reversed"] <- "Past"
colnames(PTS.logit.LDV.excl.corrtransp$var)[colnames(PTS.logit.LDV.excl.corrtransp$var)== "Lag_PTS_ai_reversed"] <- "Past"

names(PTS.logit.LDV.excl.corrwhotra $coefficients)[names(PTS.logit.LDV.excl.corrwhotra $coefficients) == "Lag_PTS_ai_reversed"] <- "Past"
rownames(PTS.logit.LDV.excl.corrwhotra $var)[rownames(PTS.logit.LDV.excl.corrwhotra $var)== "Lag_PTS_ai_reversed"] <- "Past"
colnames(PTS.logit.LDV.excl.corrwhotra $var)[colnames(PTS.logit.LDV.excl.corrwhotra $var)== "Lag_PTS_ai_reversed"] <- "Past"

names(PTS.logit.LDV.excl.corrdeposi$coefficients)[names(PTS.logit.LDV.excl.corrdeposi$coefficients) == "Lag_PTS_ai_reversed"] <- "Past"
rownames(PTS.logit.LDV.excl.corrdeposi$var)[rownames(PTS.logit.LDV.excl.corrdeposi$var)== "Lag_PTS_ai_reversed"] <- "Past"
colnames(PTS.logit.LDV.excl.corrdeposi$var)[colnames(PTS.logit.LDV.excl.corrdeposi$var)== "Lag_PTS_ai_reversed"] <- "Past"

names(PTS.logit.LDV.excl.corrpetrol$coefficients)[names(PTS.logit.LDV.excl.corrpetrol$coefficients) == "Lag_PTS_ai_reversed"] <- "Past"
rownames(PTS.logit.LDV.excl.corrpetrol$var)[rownames(PTS.logit.LDV.excl.corrpetrol$var)== "Lag_PTS_ai_reversed"] <- "Past"
colnames(PTS.logit.LDV.excl.corrpetrol$var)[colnames(PTS.logit.LDV.excl.corrpetrol$var)== "Lag_PTS_ai_reversed"] <- "Past"

names(PTS.logit.LDV.excl.corrmining$coefficients)[names(PTS.logit.LDV.excl.corrmining$coefficients) == "Lag_PTS_ai_reversed"] <- "Past"
rownames(PTS.logit.LDV.excl.corrmining$var)[rownames(PTS.logit.LDV.excl.corrmining$var)== "Lag_PTS_ai_reversed"] <- "Past"
colnames(PTS.logit.LDV.excl.corrmining$var)[colnames(PTS.logit.LDV.excl.corrmining$var)== "Lag_PTS_ai_reversed"] <- "Past"

names(PTS.logit.LDV.excl.corr$coefficients)[names(PTS.logit.LDV.excl.corr$coefficients) == "Lag_PTS_ai_reversed"] <- "Past"
rownames(PTS.logit.LDV.excl.corr$var)[rownames(PTS.logit.LDV.excl.corr$var)== "Lag_PTS_ai_reversed"] <- "Past"
colnames(PTS.logit.LDV.excl.corr$var)[colnames(PTS.logit.LDV.excl.corr$var)== "Lag_PTS_ai_reversed"] <- "Past"



#?fariss renaming
names(res.po.exclfood$coefficients)[names(res.po.exclfood$coefficients) == "Lag_Fariss_HR"] <- "Past"
rownames(res.po.exclfood$vcov)[rownames(res.po.exclfood$vcov)== "Lag_Fariss_HR"] <- "Past"
colnames(res.po.exclfood$vcov)[colnames(res.po.exclfood$vcov)== "Lag_Fariss_HR"] <- "Past"

names(res.po.exclchem$coefficients)[names(res.po.exclchem$coefficients) == "Lag_Fariss_HR"] <- "Past"
rownames(res.po.exclchem$vcov)[rownames(res.po.exclchem$vcov)== "Lag_Fariss_HR"] <- "Past"
colnames(res.po.exclchem$vcov)[colnames(res.po.exclchem$vcov)== "Lag_Fariss_HR"] <- "Past"

names(res.po.exclmetal$coefficients)[names(res.po.exclmetal$coefficients) == "Lag_Fariss_HR"] <- "Past"
rownames(res.po.exclmetal$vcov)[rownames(res.po.exclmetal$vcov)== "Lag_Fariss_HR"] <- "Past"
colnames(res.po.exclmetal$vcov)[colnames(res.po.exclmetal$vcov)== "Lag_Fariss_HR"] <- "Past"

names(res.po.exclmachin$coefficients)[names(res.po.exclmachin$coefficients) == "Lag_Fariss_HR"] <- "Past"
rownames(res.po.exclmachin$vcov)[rownames(res.po.exclmachin$vcov)== "Lag_Fariss_HR"] <- "Past"
colnames(res.po.exclmachin$vcov)[colnames(res.po.exclmachin$vcov)== "Lag_Fariss_HR"] <- "Past"

names(res.po.exclelect$coefficients)[names(res.po.exclelect$coefficients) == "Lag_Fariss_HR"] <- "Past"
rownames(res.po.exclelect$vcov)[rownames(res.po.exclelect$vcov)== "Lag_Fariss_HR"] <- "Past"
colnames(res.po.exclelect$vcov)[colnames(res.po.exclelect$vcov)== "Lag_Fariss_HR"] <- "Past"

names(res.po.excltransp$coefficients)[names(res.po.excltransp$coefficients) == "Lag_Fariss_HR"] <- "Past"
rownames(res.po.excltransp$vcov)[rownames(res.po.excltransp$vcov)== "Lag_Fariss_HR"] <- "Past"
colnames(res.po.excltransp$vcov)[colnames(res.po.excltransp$vcov)== "Lag_Fariss_HR"] <- "Past"

names(res.po.exclwhotra$coefficients)[names(res.po.exclwhotra$coefficients) == "Lag_Fariss_HR"] <- "Past"
rownames(res.po.exclwhotra$vcov)[rownames(res.po.exclwhotra$vcov)== "Lag_Fariss_HR"] <- "Past"
colnames(res.po.exclwhotra$vcov)[colnames(res.po.exclwhotra$vcov)== "Lag_Fariss_HR"] <- "Past"

names(res.po.excldeposi$coefficients)[names(res.po.excldeposi$coefficients) == "Lag_Fariss_HR"] <- "Past"
rownames(res.po.excldeposi$vcov)[rownames(res.po.excldeposi$vcov)== "Lag_Fariss_HR"] <- "Past"
colnames(res.po.excldeposi$vcov)[colnames(res.po.excldeposi$vcov)== "Lag_Fariss_HR"] <- "Past"

names(res.po.exclpetrol$coefficients)[names(res.po.exclpetrol$coefficients) == "Lag_Fariss_HR"] <- "Past"
rownames(res.po.exclpetrol$vcov)[rownames(res.po.exclpetrol$vcov)== "Lag_Fariss_HR"] <- "Past"
colnames(res.po.exclpetrol$vcov)[colnames(res.po.exclpetrol$vcov)== "Lag_Fariss_HR"] <- "Past"

names(res.po.exclmining$coefficients)[names(res.po.exclmining$coefficients) == "Lag_Fariss_HR"] <- "Past"
rownames(res.po.exclmining$vcov)[rownames(res.po.exclmining$vcov)== "Lag_Fariss_HR"] <- "Past"
colnames(res.po.exclmining$vcov)[colnames(res.po.exclmining$vcov)== "Lag_Fariss_HR"] <- "Past"

names(res.po.excl$coefficients)[names(res.po.excl$coefficients) == "Lag_Fariss_HR"] <- "Past"
rownames(res.po.excl$vcov)[rownames(res.po.excl$vcov)== "Lag_Fariss_HR"] <- "Past"
colnames(res.po.excl$vcov)[colnames(res.po.excl$vcov)== "Lag_Fariss_HR"] <- "Past"



#?build tables with stargazer CIRI
varnames <- c("Food FDI","Chemical FDI","Metal FDI","Machinery FDI","Electrical FDI","Transport FDI", "Wholesale FDI","Depository FDI", "Trade", "GDP p. capita", "Population", "Democracy", "Conflict", "Past")
dvnames <- c("CIRI Integrity")
stargazer(CIRI.logit.LDV.excl.corrfood,CIRI.logit.LDV.excl.corrchem,CIRI.logit.LDV.excl.corrmetal,CIRI.logit.LDV.excl.corrmachin,CIRI.logit.LDV.excl.correlect,CIRI.logit.LDV.excl.corrtransp,CIRI.logit.LDV.excl.corrwhotra,CIRI.logit.LDV.excl.corrdeposi,CIRI.logit.LDV.excl.corr,
          covariate.labels=varnames, dep.var.labels=dvnames, 
          column.labels   = c("Sectoral FDI are employed separately in the models", "All"),
          column.separate = c(8, 1),
          omit=c("y>=1","y>=2","y>=3","y>=4","y>=5","y>=6","y>=7","y>=8","factor","Constant","(Intercept)"), 
          omit.stat = c("f","ser","chi2"),
          nobs=T,title="Sectoral FDI and CIRI Physical Integrity Rights (sectors entered separately)",no.space=T,font.size="scriptsize", notes=c("Ordered logit with country-clustered standard errors. OLS with panel corrected standard errors.","All FDI is divided by GDP, then logged. GDP p. capita, population and trade are logged.","Past is the lagged dependent variable (LDV)."),notes.align="l",type="text",float.env="sidewaystable",label="Sectoral.separately.CIRI.tex",out="Sectoral.separately.CIRI.tex")

# build tables with stargazer PTS Amnesty
varnames <- c("Food FDI","Chemical FDI","Metal FDI","Machinery FDI","Electrical FDI","Transport FDI", "Wholesale FDI","Depository FDI", "Trade", "GDP p. capita", "Population", "Democracy", "Conflict", "Past")
dvnames <- c("PTS Amnesty")
stargazer(PTS.logit.LDV.excl.corrfood,PTS.logit.LDV.excl.corrchem,PTS.logit.LDV.excl.corrmetal,PTS.logit.LDV.excl.corrmachin,PTS.logit.LDV.excl.correlect,PTS.logit.LDV.excl.corrtransp,PTS.logit.LDV.excl.corrwhotra,PTS.logit.LDV.excl.corrdeposi,PTS.logit.LDV.excl.corr,
          covariate.labels=varnames, dep.var.labels=dvnames, 
          column.labels   = c("Sectoral FDI are employed separately in the models", "All"),
          column.separate = c(8, 1),
          omit=c("y>=1","y>=2","y>=3","y>=4","y>=5","y>=6","y>=7","y>=8","factor","Constant","(Intercept)"), 
          omit.stat = c("f","ser","chi2"),
          nobs=T,title="Sectoral FDI and PTS Amnesty Scores (sectors entered separately)",no.space=T,font.size="scriptsize", notes=c("Ordered logit with country-clustered standard errors. OLS with panel corrected standard errors.","All FDI is divided by GDP, then logged. GDP p. capita, population and trade are logged.","Past is the lagged dependent variable (LDV)."),notes.align="l",type="text",float.env="sidewaystable",label="Sectoral.separately.PTS.tex",out="Sectoral.separately.PTS.tex")

#?build tables with stargazer Latent Scores
varnames <- c("Food FDI","Chemical FDI","Metal FDI","Machinery FDI","Electrical FDI","Transport FDI", "Wholesale FDI","Depository FDI", "Trade", "GDP p. capita", "Population", "Democracy", "Conflict", "Past")
dvnames <- c("Latent Scores")
stargazer(res.po.exclfood,res.po.exclchem, res.po.exclmetal, res.po.exclmachin, res.po.exclelect, res.po.excltransp, res.po.exclwhotra, res.po.excldeposi, res.po.excl,          
          covariate.labels=varnames, dep.var.labels=dvnames, 
          column.labels   = c("Sectoral FDI are employed separately in the models", "All"),
          column.separate = c(8, 1),
          omit=c("y>=1","y>=2","y>=3","y>=4","y>=5","y>=6","y>=7","y>=8","factor","Constant","(Intercept)"), 
          omit.stat = c("f","ser","chi2"),
          nobs=T,title="Sectoral FDI and Fariss Latent Scores (sectors entered separately)",no.space=T,font.size="scriptsize", notes=c("Ordered logit with country-clustered standard errors. OLS with panel corrected standard errors.","All FDI is divided by GDP, then logged. GDP p. capita, population and trade are logged.","Past is the lagged dependent variable (LDV)."),notes.align="l",type="text",float.env="sidewaystable",label="Sectoral.separately.Fariss.tex",out="Sectoral.separately.Fariss.tex")














# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 
# Tables: 
# "Sectoral FDI and CIRI Physical Integrity Rights, Sector Size Incl. (sectors entered separately)"
# "Sectoral FDI and PTS Amnesty Scores, Sector Size Incl. (sectors entered separately)"
# "Sectoral FDI and Fariss Latent Scores, Sector Size Incl. (sectors entered separately)
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 


#############################################################################################################################################

# Data entering FDI separately, including "Lag_log_Employment_Services", "Lag_log_Employment_Industry"
# dim(t.nono) # 4582  423
data.CIRI.PHY.excl.LDVfood <- na.omit(t.nono[,cbind("CIRI_PHYSINT","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_foodpGDP","Lag_CIRI_PHYSINT", "country","year","Lag_log_Employment_Services", "Lag_log_Employment_Industry")])
data.CIRI.PHY.excl.LDVchem <- na.omit(t.nono[,cbind("CIRI_PHYSINT","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_chemicalpGDP","Lag_CIRI_PHYSINT", "country","year","Lag_log_Employment_Services", "Lag_log_Employment_Industry")])
data.CIRI.PHY.excl.LDVmetal <- na.omit(t.nono[,cbind("CIRI_PHYSINT","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_prim_fab_metalpGDP","Lag_CIRI_PHYSINT", "country","year","Lag_log_Employment_Services", "Lag_log_Employment_Industry")])
data.CIRI.PHY.excl.LDVmachin <- na.omit(t.nono[,cbind("CIRI_PHYSINT","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_machinerypGDP","Lag_CIRI_PHYSINT", "country","year","Lag_log_Employment_Services", "Lag_log_Employment_Industry")])
data.CIRI.PHY.excl.LDVelect <- na.omit(t.nono[,cbind("CIRI_PHYSINT","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_electricalpGDP","Lag_CIRI_PHYSINT", "country","year","Lag_log_Employment_Services", "Lag_log_Employment_Industry")])
data.CIRI.PHY.excl.LDVtransp <- na.omit(t.nono[,cbind("CIRI_PHYSINT","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_transportpGDP","Lag_CIRI_PHYSINT", "country","year","Lag_log_Employment_Services", "Lag_log_Employment_Industry")])
data.CIRI.PHY.excl.LDVwhotra <- na.omit(t.nono[,cbind("CIRI_PHYSINT","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_whole_tradepGDP","Lag_CIRI_PHYSINT", "country","year","Lag_log_Employment_Services", "Lag_log_Employment_Industry")])
data.CIRI.PHY.excl.LDVdeposi <- na.omit(t.nono[,cbind("CIRI_PHYSINT","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_depositorypGDP","Lag_CIRI_PHYSINT", "country","year","Lag_log_Employment_Services", "Lag_log_Employment_Industry")])
#add petrol and mining
data.CIRI.PHY.excl.LDVpetrol <- na.omit(t.nono[,cbind("CIRI_PHYSINT","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_petrolpGDP","Lag_CIRI_PHYSINT", "country","year","Lag_log_Employment_Services", "Lag_log_Employment_Industry")])
data.CIRI.PHY.excl.LDVmining <- na.omit(t.nono[,cbind("CIRI_PHYSINT","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_miningpGDP","Lag_CIRI_PHYSINT", "country","year","Lag_log_Employment_Services", "Lag_log_Employment_Industry")])
data.CIRI.PHY.excl.LDV <- na.omit(t.nono[,cbind("CIRI_PHYSINT","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_foodpGDP","Lag_lognonnegUS_fdi_chemicalpGDP","Lag_lognonnegUS_fdi_prim_fab_metalpGDP","Lag_lognonnegUS_fdi_machinerypGDP","Lag_lognonnegUS_fdi_electricalpGDP","Lag_lognonnegUS_fdi_transportpGDP","Lag_lognonnegUS_fdi_whole_tradepGDP","Lag_lognonnegUS_fdi_depositorypGDP","Lag_CIRI_PHYSINT", "country","year","Lag_log_Employment_Services", "Lag_log_Employment_Industry")])


data.PTS.excl.LDVfood <- na.omit(t.nono[,cbind("PTS_ai_reversed","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_foodpGDP","Lag_PTS_ai_reversed", "country","year","Lag_log_Employment_Services", "Lag_log_Employment_Industry")])
data.PTS.excl.LDVchem <- na.omit(t.nono[,cbind("PTS_ai_reversed","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_chemicalpGDP","Lag_PTS_ai_reversed", "country","year","Lag_log_Employment_Services", "Lag_log_Employment_Industry")])
data.PTS.excl.LDVmetal <- na.omit(t.nono[,cbind("PTS_ai_reversed","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_prim_fab_metalpGDP","Lag_PTS_ai_reversed", "country","year","Lag_log_Employment_Services", "Lag_log_Employment_Industry")])
data.PTS.excl.LDVmachin <- na.omit(t.nono[,cbind("PTS_ai_reversed","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_machinerypGDP","Lag_PTS_ai_reversed", "country","year","Lag_log_Employment_Services", "Lag_log_Employment_Industry")])
data.PTS.excl.LDVelect <- na.omit(t.nono[,cbind("PTS_ai_reversed","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_electricalpGDP","Lag_PTS_ai_reversed", "country","year","Lag_log_Employment_Services", "Lag_log_Employment_Industry")])
data.PTS.excl.LDVtransp <- na.omit(t.nono[,cbind("PTS_ai_reversed","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_transportpGDP","Lag_PTS_ai_reversed", "country","year","Lag_log_Employment_Services", "Lag_log_Employment_Industry")])
data.PTS.excl.LDVwhotra <- na.omit(t.nono[,cbind("PTS_ai_reversed","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_whole_tradepGDP","Lag_PTS_ai_reversed", "country","year","Lag_log_Employment_Services", "Lag_log_Employment_Industry")])
data.PTS.excl.LDVdeposi <- na.omit(t.nono[,cbind("PTS_ai_reversed","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_depositorypGDP","Lag_PTS_ai_reversed", "country","year","Lag_log_Employment_Services", "Lag_log_Employment_Industry")])
#add petrol and mining
data.PTS.excl.LDVpetrol <- na.omit(t.nono[,cbind("PTS_ai_reversed","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_petrolpGDP","Lag_PTS_ai_reversed", "country","year","Lag_log_Employment_Services", "Lag_log_Employment_Industry")])
data.PTS.excl.LDVmining <- na.omit(t.nono[,cbind("PTS_ai_reversed","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_miningpGDP","Lag_PTS_ai_reversed", "country","year","Lag_log_Employment_Services", "Lag_log_Employment_Industry")])
data.PTS.excl.LDV  <- na.omit(t.nono[,cbind("PTS_ai_reversed","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_foodpGDP","Lag_lognonnegUS_fdi_chemicalpGDP","Lag_lognonnegUS_fdi_prim_fab_metalpGDP","Lag_lognonnegUS_fdi_machinerypGDP","Lag_lognonnegUS_fdi_electricalpGDP","Lag_lognonnegUS_fdi_transportpGDP","Lag_lognonnegUS_fdi_whole_tradepGDP","Lag_lognonnegUS_fdi_depositorypGDP","Lag_PTS_ai_reversed","country","year","Lag_log_Employment_Services", "Lag_log_Employment_Industry")])

data.Fariss.excl.LDVfood <- na.omit(t.nono[,cbind("Fariss_HR","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_foodpGDP","Lag_Fariss_HR", "country","year","Lag_log_Employment_Services", "Lag_log_Employment_Industry")])
data.Fariss.excl.LDVfood <- as.data.frame(data.Fariss.excl.LDVfood)

data.Fariss.excl.LDVchem <- na.omit(t.nono[,cbind("Fariss_HR","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_chemicalpGDP","Lag_Fariss_HR", "country","year","Lag_log_Employment_Services", "Lag_log_Employment_Industry")])
data.Fariss.excl.LDVchem <- as.data.frame(data.Fariss.excl.LDVchem)

data.Fariss.excl.LDVmetal <- na.omit(t.nono[,cbind("Fariss_HR","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_prim_fab_metalpGDP","Lag_Fariss_HR", "country","year","Lag_log_Employment_Services", "Lag_log_Employment_Industry")])
data.Fariss.excl.LDVmetal <- as.data.frame(data.Fariss.excl.LDVmetal)

data.Fariss.excl.LDVmachin <- na.omit(t.nono[,cbind("Fariss_HR","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_machinerypGDP","Lag_Fariss_HR", "country","year","Lag_log_Employment_Services", "Lag_log_Employment_Industry")])
data.Fariss.excl.LDVmachin <- as.data.frame(data.Fariss.excl.LDVmachin)

data.Fariss.excl.LDVelect <- na.omit(t.nono[,cbind("Fariss_HR","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_electricalpGDP","Lag_Fariss_HR", "country","year","Lag_log_Employment_Services", "Lag_log_Employment_Industry")])
data.Fariss.excl.LDVelect <- as.data.frame(data.Fariss.excl.LDVelect)

data.Fariss.excl.LDVtransp <- na.omit(t.nono[,cbind("Fariss_HR","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_transportpGDP","Lag_Fariss_HR", "country","year","Lag_log_Employment_Services", "Lag_log_Employment_Industry")])
data.Fariss.excl.LDVtransp <- as.data.frame(data.Fariss.excl.LDVtransp)

data.Fariss.excl.LDVwhotra <- na.omit(t.nono[,cbind("Fariss_HR","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_whole_tradepGDP","Lag_Fariss_HR", "country","year","Lag_log_Employment_Services", "Lag_log_Employment_Industry")])
data.Fariss.excl.LDVwhotra <- as.data.frame(data.Fariss.excl.LDVwhotra)

data.Fariss.excl.LDVdeposi <- na.omit(t.nono[,cbind("Fariss_HR","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_depositorypGDP","Lag_Fariss_HR", "country","year","Lag_log_Employment_Services", "Lag_log_Employment_Industry")])
data.Fariss.excl.LDVdeposi <- as.data.frame(data.Fariss.excl.LDVdeposi)

#add petrol and mining
data.Fariss.excl.LDVpetrol <- na.omit(t.nono[,cbind("Fariss_HR","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_petrolpGDP","Lag_Fariss_HR", "country","year","Lag_log_Employment_Services", "Lag_log_Employment_Industry")])
data.Fariss.excl.LDVpetrol <- as.data.frame(data.Fariss.excl.LDVpetrol)

data.Fariss.excl.LDVmining <- na.omit(t.nono[,cbind("Fariss_HR","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_miningpGDP","Lag_Fariss_HR", "country","year","Lag_log_Employment_Services", "Lag_log_Employment_Industry")])
data.Fariss.excl.LDVmining <- as.data.frame(data.Fariss.excl.LDVmining)

data.Fariss.excl.LDV <- na.omit(t.nono[,cbind("Fariss_HR","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_foodpGDP","Lag_lognonnegUS_fdi_chemicalpGDP","Lag_lognonnegUS_fdi_prim_fab_metalpGDP","Lag_lognonnegUS_fdi_machinerypGDP","Lag_lognonnegUS_fdi_electricalpGDP","Lag_lognonnegUS_fdi_transportpGDP","Lag_lognonnegUS_fdi_whole_tradepGDP","Lag_lognonnegUS_fdi_depositorypGDP","Lag_Fariss_HR","country","year","Lag_log_Employment_Services", "Lag_log_Employment_Industry")])
data.Fariss.excl.LDV <- as.data.frame(data.Fariss.excl.LDV)


#?Models each sector separately, including sector size "Lag_log_Employment_Services", "Lag_log_Employment_Industry"

# MODELS ####
# Models CIRI and all sectors separately
CIRI.logit.LDV.exclfood <- lrm(CIRI_PHYSINT ~ Lag_lognonnegUS_fdi_foodpGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_log_Employment_Services + Lag_log_Employment_Industry + Lag_CIRI_PHYSINT,data=data.CIRI.PHY.excl.LDVfood,x=TRUE, y=TRUE)
CIRI.logit.LDV.excl.corrfood <- robcov(CIRI.logit.LDV.exclfood,data.CIRI.PHY.excl.LDVfood$country) #robust errors  

CIRI.logit.LDV.exclchem <- lrm(CIRI_PHYSINT ~ Lag_lognonnegUS_fdi_chemicalpGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_log_Employment_Services + Lag_log_Employment_Industry+ Lag_CIRI_PHYSINT,data=data.CIRI.PHY.excl.LDVchem,x=TRUE, y=TRUE)
CIRI.logit.LDV.excl.corrchem <- robcov(CIRI.logit.LDV.exclchem,data.CIRI.PHY.excl.LDVchem$country) #robust errors  

CIRI.logit.LDV.exclmetal <- lrm(CIRI_PHYSINT ~ Lag_lognonnegUS_fdi_prim_fab_metalpGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_log_Employment_Services + Lag_log_Employment_Industry+ Lag_CIRI_PHYSINT,data=data.CIRI.PHY.excl.LDVmetal,x=TRUE, y=TRUE)
CIRI.logit.LDV.excl.corrmetal <- robcov(CIRI.logit.LDV.exclmetal,data.CIRI.PHY.excl.LDVmetal$country) #robust errors  

CIRI.logit.LDV.exclmachin <- lrm(CIRI_PHYSINT ~ Lag_lognonnegUS_fdi_machinerypGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_log_Employment_Services + Lag_log_Employment_Industry+ Lag_CIRI_PHYSINT,data=data.CIRI.PHY.excl.LDVmachin,x=TRUE, y=TRUE)
CIRI.logit.LDV.excl.corrmachin <- robcov(CIRI.logit.LDV.exclmachin,data.CIRI.PHY.excl.LDVmachin$country) #robust errors  

CIRI.logit.LDV.exclelect <- lrm(CIRI_PHYSINT ~ Lag_lognonnegUS_fdi_electricalpGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_log_Employment_Services + Lag_log_Employment_Industry+ Lag_CIRI_PHYSINT,data=data.CIRI.PHY.excl.LDVelect,x=TRUE, y=TRUE)
CIRI.logit.LDV.excl.correlect <- robcov(CIRI.logit.LDV.exclelect,data.CIRI.PHY.excl.LDVelect$country) #robust errors  

CIRI.logit.LDV.excltransp <- lrm(CIRI_PHYSINT ~ Lag_lognonnegUS_fdi_transportpGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_log_Employment_Services + Lag_log_Employment_Industry+ Lag_CIRI_PHYSINT,data=data.CIRI.PHY.excl.LDVtransp,x=TRUE, y=TRUE)
CIRI.logit.LDV.excl.corrtransp <- robcov(CIRI.logit.LDV.excltransp,data.CIRI.PHY.excl.LDVtransp$country) #robust errors  

CIRI.logit.LDV.exclwhotra <- lrm(CIRI_PHYSINT ~ Lag_lognonnegUS_fdi_whole_tradepGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_log_Employment_Services + Lag_log_Employment_Industry+ Lag_CIRI_PHYSINT,data=data.CIRI.PHY.excl.LDVwhotra,x=TRUE, y=TRUE)
CIRI.logit.LDV.excl.corrwhotra <- robcov(CIRI.logit.LDV.exclwhotra,data.CIRI.PHY.excl.LDVwhotra$country) #robust errors  

CIRI.logit.LDV.excldeposi <- lrm(CIRI_PHYSINT ~ Lag_lognonnegUS_fdi_depositorypGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_log_Employment_Services + Lag_log_Employment_Industry+ Lag_CIRI_PHYSINT,data=data.CIRI.PHY.excl.LDVdeposi,x=TRUE, y=TRUE)
CIRI.logit.LDV.excl.corrdeposi <- robcov(CIRI.logit.LDV.excldeposi,data.CIRI.PHY.excl.LDVdeposi$country) #robust errors  

CIRI.logit.LDV.exclpetrol <- lrm(CIRI_PHYSINT ~ Lag_lognonnegUS_fdi_petrolpGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_log_Employment_Services + Lag_log_Employment_Industry+ Lag_CIRI_PHYSINT,data=data.CIRI.PHY.excl.LDVpetrol,x=TRUE, y=TRUE)
CIRI.logit.LDV.excl.corrpetrol <- robcov(CIRI.logit.LDV.exclpetrol,data.CIRI.PHY.excl.LDVpetrol$country) #robust errors  

CIRI.logit.LDV.exclmining<- lrm(CIRI_PHYSINT ~ Lag_lognonnegUS_fdi_miningpGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_log_Employment_Services + Lag_log_Employment_Industry+ Lag_CIRI_PHYSINT,data=data.CIRI.PHY.excl.LDVmining,x=TRUE, y=TRUE)
CIRI.logit.LDV.excl.corrmining <- robcov(CIRI.logit.LDV.exclmining,data.CIRI.PHY.excl.LDVmining$country) #robust errors  

CIRI.logit.LDV.excl <- lrm(CIRI_PHYSINT ~ Lag_lognonnegUS_fdi_foodpGDP + Lag_lognonnegUS_fdi_chemicalpGDP + Lag_lognonnegUS_fdi_prim_fab_metalpGDP + Lag_lognonnegUS_fdi_machinerypGDP + Lag_lognonnegUS_fdi_electricalpGDP + Lag_lognonnegUS_fdi_transportpGDP + Lag_lognonnegUS_fdi_whole_tradepGDP + Lag_lognonnegUS_fdi_depositorypGDP+ Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_log_Employment_Services + Lag_log_Employment_Industry+ Lag_CIRI_PHYSINT,data=data.CIRI.PHY.excl.LDV  ,x=TRUE, y=TRUE)
CIRI.logit.LDV.excl.corr <- robcov(CIRI.logit.LDV.excl,data.CIRI.PHY.excl.LDV$country) #robust errors  


# Models PTS and all sectors separately
PTS.logit.LDV.exclfood <- lrm(PTS_ai_reversed ~ Lag_lognonnegUS_fdi_foodpGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_log_Employment_Services + Lag_log_Employment_Industry+ Lag_PTS_ai_reversed,data=data.PTS.excl.LDVfood,x=TRUE, y=TRUE)
PTS.logit.LDV.excl.corrfood <- robcov(PTS.logit.LDV.exclfood,data.PTS.excl.LDVfood$country) #robust errors  

PTS.logit.LDV.exclchem <- lrm(PTS_ai_reversed ~ Lag_lognonnegUS_fdi_chemicalpGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_log_Employment_Services + Lag_log_Employment_Industry+ Lag_PTS_ai_reversed,data=data.PTS.excl.LDVchem,x=TRUE, y=TRUE)
PTS.logit.LDV.excl.corrchem <- robcov(PTS.logit.LDV.exclchem,data.PTS.excl.LDVchem$country) #robust errors  

PTS.logit.LDV.exclmetal <- lrm(PTS_ai_reversed ~ Lag_lognonnegUS_fdi_prim_fab_metalpGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_log_Employment_Services + Lag_log_Employment_Industry+ Lag_PTS_ai_reversed,data=data.PTS.excl.LDVmetal,x=TRUE, y=TRUE)
PTS.logit.LDV.excl.corrmetal <- robcov(PTS.logit.LDV.exclmetal,data.PTS.excl.LDVmetal$country) #robust errors  

PTS.logit.LDV.exclmachin <- lrm(PTS_ai_reversed ~ Lag_lognonnegUS_fdi_machinerypGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_log_Employment_Services + Lag_log_Employment_Industry+ Lag_PTS_ai_reversed,data=data.PTS.excl.LDVmachin,x=TRUE, y=TRUE)
PTS.logit.LDV.excl.corrmachin <- robcov(PTS.logit.LDV.exclmachin,data.PTS.excl.LDVmachin$country) #robust errors  

PTS.logit.LDV.exclelect <- lrm(PTS_ai_reversed ~ Lag_lognonnegUS_fdi_electricalpGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_log_Employment_Services + Lag_log_Employment_Industry+ Lag_PTS_ai_reversed,data=data.PTS.excl.LDVelect,x=TRUE, y=TRUE)
PTS.logit.LDV.excl.correlect <- robcov(PTS.logit.LDV.exclelect,data.PTS.excl.LDVelect$country) #robust errors  

PTS.logit.LDV.excltransp <- lrm(PTS_ai_reversed ~ Lag_lognonnegUS_fdi_transportpGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_log_Employment_Services + Lag_log_Employment_Industry + Lag_PTS_ai_reversed,data=data.PTS.excl.LDVtransp,x=TRUE, y=TRUE)
PTS.logit.LDV.excl.corrtransp <- robcov(PTS.logit.LDV.excltransp,data.PTS.excl.LDVtransp$country) #robust errors  

PTS.logit.LDV.exclwhotra <- lrm(PTS_ai_reversed ~ Lag_lognonnegUS_fdi_whole_tradepGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_log_Employment_Services + Lag_log_Employment_Industry+ Lag_PTS_ai_reversed,data=data.PTS.excl.LDVwhotra,x=TRUE, y=TRUE)
PTS.logit.LDV.excl.corrwhotra <- robcov(PTS.logit.LDV.exclwhotra,data.PTS.excl.LDVwhotra$country) #robust errors  

PTS.logit.LDV.excldeposi <- lrm(PTS_ai_reversed ~ Lag_lognonnegUS_fdi_depositorypGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_log_Employment_Services + Lag_log_Employment_Industry+ Lag_PTS_ai_reversed,data=data.PTS.excl.LDVdeposi,x=TRUE, y=TRUE)
PTS.logit.LDV.excl.corrdeposi <- robcov(PTS.logit.LDV.excldeposi,data.PTS.excl.LDVdeposi$country) #robust errors  

PTS.logit.LDV.exclpetrol <- lrm(PTS_ai_reversed ~ Lag_lognonnegUS_fdi_petrolpGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_log_Employment_Services + Lag_log_Employment_Industry+ Lag_PTS_ai_reversed,data=data.PTS.excl.LDVpetrol,x=TRUE, y=TRUE)
PTS.logit.LDV.excl.corrpetrol <- robcov(PTS.logit.LDV.exclpetrol,data.PTS.excl.LDVpetrol$country) #robust errors  

PTS.logit.LDV.exclmining<- lrm(PTS_ai_reversed ~ Lag_lognonnegUS_fdi_miningpGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_log_Employment_Services + Lag_log_Employment_Industry+ Lag_PTS_ai_reversed,data=data.PTS.excl.LDVmining,x=TRUE, y=TRUE)
PTS.logit.LDV.excl.corrmining <- robcov(PTS.logit.LDV.exclmining,data.PTS.excl.LDVmining$country) #robust errors  

PTS.logit.LDV.excl <- lrm(PTS_ai_reversed ~ Lag_lognonnegUS_fdi_foodpGDP + Lag_lognonnegUS_fdi_chemicalpGDP + Lag_lognonnegUS_fdi_prim_fab_metalpGDP + Lag_lognonnegUS_fdi_machinerypGDP + Lag_lognonnegUS_fdi_electricalpGDP + Lag_lognonnegUS_fdi_transportpGDP + Lag_lognonnegUS_fdi_whole_tradepGDP + Lag_lognonnegUS_fdi_depositorypGDP+ Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_log_Employment_Services + Lag_log_Employment_Industry+ Lag_PTS_ai_reversed,data=data.PTS.excl.LDV,x=TRUE, y=TRUE)
PTS.logit.LDV.excl.corr <- robcov(PTS.logit.LDV.excl,data.PTS.excl.LDV$country) 


# Fariss Models each FDI separately
form   <- Fariss_HR ~ Lag_lognonnegUS_fdi_foodpGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_log_Employment_Services + Lag_log_Employment_Industry+ Lag_Fariss_HR
res.po.exclfood <- plm(form,model="pooling",index=c("country","year"),data=data.Fariss.excl.LDVfood)
## PCSE a la Beck-Katz
res.BKtime.exclfood  <- vcovBK(x=res.po.exclfood,cluster="time")
ct.time.exclfood     <- coeftest(res.po.exclfood,vcov=res.BKtime.exclfood)
res.po.exclfood$vcov <- res.BKtime.exclfood  # replacing the first uncorrected residual covariance structure with the corrected SE
print(ct.time.exclfood)

form   <- Fariss_HR ~ Lag_lognonnegUS_fdi_chemicalpGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_log_Employment_Services + Lag_log_Employment_Industry+ Lag_Fariss_HR
res.po.exclchem <- plm(form,model="pooling",index=c("country","year"),data=data.Fariss.excl.LDVchem)
## PCSE a la Beck-Katz
res.BKtime.exclchem  <- vcovBK(x=res.po.exclchem,cluster="time")
ct.time.exclchem    <- coeftest(res.po.exclchem ,vcov=res.BKtime.exclchem )
res.po.exclchem$vcov <- res.BKtime.exclchem   # replacing the first uncorrected residual covariance structure with the corrected SE
print(ct.time.exclchem)

form   <- Fariss_HR ~  Lag_lognonnegUS_fdi_prim_fab_metalpGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_log_Employment_Services + Lag_log_Employment_Industry+ Lag_Fariss_HR
res.po.exclmetal <- plm(form,model="pooling",index=c("country","year"),data=data.Fariss.excl.LDVmetal)
## PCSE a la Beck-Katz
res.BKtime.exclmetal  <- vcovBK(x=res.po.exclmetal,cluster="time")
ct.time.exclmetal    <- coeftest(res.po.exclmetal ,vcov=res.BKtime.exclmetal)
res.po.exclmetal$vcov <- res.BKtime.exclmetal   # replacing the first uncorrected residual covariance structure with the corrected SE
print(ct.time.exclmetal)

form   <- Fariss_HR ~  Lag_lognonnegUS_fdi_machinerypGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_log_Employment_Services + Lag_log_Employment_Industry+ Lag_Fariss_HR
res.po.exclmachin <- plm(form,model="pooling",index=c("country","year"),data=data.Fariss.excl.LDVmachin)
## PCSE a la Beck-Katz
res.BKtime.exclmachin  <- vcovBK(x=res.po.exclmachin,cluster="time")
ct.time.exclmachin    <- coeftest(res.po.exclmachin ,vcov=res.BKtime.exclmachin)
res.po.exclmachin$vcov <- res.BKtime.exclmachin   # replacing the first uncorrected residual covariance structure with the corrected SE
print(ct.time.exclmachin)

form   <- Fariss_HR ~  Lag_lognonnegUS_fdi_electricalpGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_log_Employment_Services + Lag_log_Employment_Industry+ Lag_Fariss_HR
res.po.exclelect <- plm(form,model="pooling",index=c("country","year"),data=data.Fariss.excl.LDVelect)
## PCSE a la Beck-Katz
res.BKtime.exclelect  <- vcovBK(x=res.po.exclelect,cluster="time")
ct.time.exclelect    <- coeftest(res.po.exclelect ,vcov=res.BKtime.exclelect)
res.po.exclelect$vcov <- res.BKtime.exclelect   # replacing the first uncorrected residual covariance structure with the corrected SE
print(ct.time.exclelect)

form   <- Fariss_HR ~  Lag_lognonnegUS_fdi_transportpGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_log_Employment_Services + Lag_log_Employment_Industry+ Lag_Fariss_HR
res.po.excltransp <- plm(form,model="pooling",index=c("country","year"),data=data.Fariss.excl.LDVtransp)
## PCSE a la Beck-Katz
res.BKtime.excltransp  <- vcovBK(x=res.po.excltransp,cluster="time")
ct.time.excltransp    <- coeftest(res.po.excltransp ,vcov=res.BKtime.excltransp)
res.po.excltransp$vcov <- res.BKtime.excltransp   # replacing the first uncorrected residual covariance structure with the corrected SE
print(ct.time.excltransp)

form   <- Fariss_HR ~  Lag_lognonnegUS_fdi_whole_tradepGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_log_Employment_Services + Lag_log_Employment_Industry+ Lag_Fariss_HR
res.po.exclwhotra <- plm(form,model="pooling",index=c("country","year"),data=data.Fariss.excl.LDVwhotra)
## PCSE a la Beck-Katz
res.BKtime.exclwhotra <- vcovBK(x=res.po.exclwhotra,cluster="time")
ct.time.exclwhotra   <- coeftest(res.po.exclwhotra,vcov=res.BKtime.exclwhotra)
res.po.exclwhotra$vcov <- res.BKtime.exclwhotra   # replacing the first uncorrected residual covariance structure with the corrected SE
print(ct.time.exclwhotra)

form   <- Fariss_HR ~  Lag_lognonnegUS_fdi_depositorypGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_log_Employment_Services + Lag_log_Employment_Industry+ Lag_Fariss_HR
res.po.excldeposi <- plm(form,model="pooling",index=c("country","year"),data=data.Fariss.excl.LDVdeposi)
## PCSE a la Beck-Katz
res.BKtime.excldeposi <- vcovBK(x=res.po.excldeposi,cluster="time")
ct.time.excldeposi   <- coeftest(res.po.excldeposi,vcov=res.BKtime.excldeposi)
res.po.excldeposi$vcov <- res.BKtime.excldeposi   # replacing the first uncorrected residual covariance structure with the corrected SE
print(ct.time.excldeposi)

form   <- Fariss_HR ~  Lag_lognonnegUS_fdi_petrolpGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_log_Employment_Services + Lag_log_Employment_Industry+ Lag_Fariss_HR
res.po.exclpetrol <- plm(form,model="pooling",index=c("country","year"),data=data.Fariss.excl.LDVpetrol)
## PCSE a la Beck-Katz
res.BKtime.exclpetrol <- vcovBK(x=res.po.exclpetrol,cluster="time")
ct.time.exclpetrol   <- coeftest(res.po.exclpetrol,vcov=res.BKtime.exclpetrol)
res.po.exclpetrol$vcov <- res.BKtime.exclpetrol   # replacing the first uncorrected residual covariance structure with the corrected SE
print(ct.time.exclpetrol)

form   <- Fariss_HR ~  Lag_lognonnegUS_fdi_miningpGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_log_Employment_Services + Lag_log_Employment_Industry+ Lag_Fariss_HR
res.po.exclmining <- plm(form,model="pooling",index=c("country","year"),data=data.Fariss.excl.LDVmining)
## PCSE a la Beck-Katz
res.BKtime.exclmining <- vcovBK(x=res.po.exclmining,cluster="time")
ct.time.exclmining   <- coeftest(res.po.exclmining,vcov=res.BKtime.exclmining)
res.po.exclmining$vcov <- res.BKtime.exclmining   # replacing the first uncorrected residual covariance structure with the corrected SE
print(ct.time.exclmining)

form   <- Fariss_HR ~  Lag_lognonnegUS_fdi_foodpGDP + Lag_lognonnegUS_fdi_chemicalpGDP + Lag_lognonnegUS_fdi_prim_fab_metalpGDP + Lag_lognonnegUS_fdi_machinerypGDP + Lag_lognonnegUS_fdi_electricalpGDP + Lag_lognonnegUS_fdi_transportpGDP + Lag_lognonnegUS_fdi_whole_tradepGDP + Lag_lognonnegUS_fdi_depositorypGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_log_Employment_Services + Lag_log_Employment_Industry+ Lag_Fariss_HR
res.po.excl <- plm(form,model="pooling",index=c("country","year"),data=data.Fariss.excl.LDV)
## PCSE a la Beck-Katz
res.BKtime.excl  <- vcovBK(x=res.po.excl,cluster="time")
ct.time.excl     <- coeftest(res.po.excl,vcov=res.BKtime.excl)
res.po.excl$vcov <- res.BKtime.excl  # replacing the first uncorrected residual covariance structure with the corrected SE
print(ct.time.excl)


# Create three tables (CIRI, PTS, Fariss) with these results, last column is what T5 used to be (all sectors in one exclusive petrol and mining)
# remember that I am using the same R output names as before (without sectoral employment; make sure all models ran correctly or it will pull the previously created R object)
#?rename lagged dependent variable in R output objects so they will go in the same row called "Past" in results table
names(CIRI.logit.LDV.excl.corrfood$coefficients)[names(CIRI.logit.LDV.excl.corrfood$coefficients) == "Lag_CIRI_PHYSINT"] <- "Past"
rownames(CIRI.logit.LDV.excl.corrfood$var)[rownames(CIRI.logit.LDV.excl.corrfood$var)== "Lag_CIRI_PHYSINT"] <- "Past"
colnames(CIRI.logit.LDV.excl.corrfood$var)[colnames(CIRI.logit.LDV.excl.corrfood$var)== "Lag_CIRI_PHYSINT"] <- "Past"

names(CIRI.logit.LDV.excl.corrchem$coefficients)[names(CIRI.logit.LDV.excl.corrchem$coefficients) == "Lag_CIRI_PHYSINT"] <- "Past"
rownames(CIRI.logit.LDV.excl.corrchem$var)[rownames(CIRI.logit.LDV.excl.corrchem$var)== "Lag_CIRI_PHYSINT"] <- "Past"
colnames(CIRI.logit.LDV.excl.corrchem$var)[colnames(CIRI.logit.LDV.excl.corrchem$var)== "Lag_CIRI_PHYSINT"] <- "Past"

names(CIRI.logit.LDV.excl.corrmetal$coefficients)[names(CIRI.logit.LDV.excl.corrmetal$coefficients) == "Lag_CIRI_PHYSINT"] <- "Past"
rownames(CIRI.logit.LDV.excl.corrmetal$var)[rownames(CIRI.logit.LDV.excl.corrmetal$var)== "Lag_CIRI_PHYSINT"] <- "Past"
colnames(CIRI.logit.LDV.excl.corrmetal$var)[colnames(CIRI.logit.LDV.excl.corrmetal$var)== "Lag_CIRI_PHYSINT"] <- "Past"

names(CIRI.logit.LDV.excl.corrmachin$coefficients)[names(CIRI.logit.LDV.excl.corrmachin$coefficients) == "Lag_CIRI_PHYSINT"] <- "Past"
rownames(CIRI.logit.LDV.excl.corrmachin$var)[rownames(CIRI.logit.LDV.excl.corrmachin$var)== "Lag_CIRI_PHYSINT"] <- "Past"
colnames(CIRI.logit.LDV.excl.corrmachin$var)[colnames(CIRI.logit.LDV.excl.corrmachin$var)== "Lag_CIRI_PHYSINT"] <- "Past"

names(CIRI.logit.LDV.excl.correlect$coefficients)[names(CIRI.logit.LDV.excl.correlect$coefficients) == "Lag_CIRI_PHYSINT"] <- "Past"
rownames(CIRI.logit.LDV.excl.correlect$var)[rownames(CIRI.logit.LDV.excl.correlect$var)== "Lag_CIRI_PHYSINT"] <- "Past"
colnames(CIRI.logit.LDV.excl.correlect$var)[colnames(CIRI.logit.LDV.excl.correlect$var)== "Lag_CIRI_PHYSINT"] <- "Past"

names(CIRI.logit.LDV.excl.corrtransp$coefficients)[names(CIRI.logit.LDV.excl.corrtransp$coefficients) == "Lag_CIRI_PHYSINT"] <- "Past"
rownames(CIRI.logit.LDV.excl.corrtransp$var)[rownames(CIRI.logit.LDV.excl.corrtransp$var)== "Lag_CIRI_PHYSINT"] <- "Past"
colnames(CIRI.logit.LDV.excl.corrtransp$var)[colnames(CIRI.logit.LDV.excl.corrtransp$var)== "Lag_CIRI_PHYSINT"] <- "Past"

names(CIRI.logit.LDV.excl.corrwhotra$coefficients)[names(CIRI.logit.LDV.excl.corrwhotra$coefficients) == "Lag_CIRI_PHYSINT"] <- "Past"
rownames(CIRI.logit.LDV.excl.corrwhotra$var)[rownames(CIRI.logit.LDV.excl.corrwhotra$var)== "Lag_CIRI_PHYSINT"] <- "Past"
colnames(CIRI.logit.LDV.excl.corrwhotra$var)[colnames(CIRI.logit.LDV.excl.corrwhotra$var)== "Lag_CIRI_PHYSINT"] <- "Past"

names(CIRI.logit.LDV.excl.corrdeposi$coefficients)[names(CIRI.logit.LDV.excl.corrdeposi$coefficients) == "Lag_CIRI_PHYSINT"] <- "Past"
rownames(CIRI.logit.LDV.excl.corrdeposi$var)[rownames(CIRI.logit.LDV.excl.corrdeposi$var)== "Lag_CIRI_PHYSINT"] <- "Past"
colnames(CIRI.logit.LDV.excl.corrdeposi$var)[colnames(CIRI.logit.LDV.excl.corrdeposi$var)== "Lag_CIRI_PHYSINT"] <- "Past"

names(CIRI.logit.LDV.excl.corrpetrol$coefficients)[names(CIRI.logit.LDV.excl.corrpetrol$coefficients) == "Lag_CIRI_PHYSINT"] <- "Past"
rownames(CIRI.logit.LDV.excl.corrpetrol$var)[rownames(CIRI.logit.LDV.excl.corrpetrol$var)== "Lag_CIRI_PHYSINT"] <- "Past"
colnames(CIRI.logit.LDV.excl.corrpetrol$var)[colnames(CIRI.logit.LDV.excl.corrpetrol$var)== "Lag_CIRI_PHYSINT"] <- "Past"

names(CIRI.logit.LDV.excl.corrmining$coefficients)[names(CIRI.logit.LDV.excl.corrmining$coefficients) == "Lag_CIRI_PHYSINT"] <- "Past"
rownames(CIRI.logit.LDV.excl.corrmining$var)[rownames(CIRI.logit.LDV.excl.corrmining$var)== "Lag_CIRI_PHYSINT"] <- "Past"
colnames(CIRI.logit.LDV.excl.corrmining$var)[colnames(CIRI.logit.LDV.excl.corrmining$var)== "Lag_CIRI_PHYSINT"] <- "Past"

names(CIRI.logit.LDV.excl.corr$coefficients)[names(CIRI.logit.LDV.excl.corr$coefficients) == "Lag_CIRI_PHYSINT"] <- "Past"
rownames(CIRI.logit.LDV.excl.corr$var)[rownames(CIRI.logit.LDV.excl.corr$var)== "Lag_CIRI_PHYSINT"] <- "Past"
colnames(CIRI.logit.LDV.excl.corr$var)[colnames(CIRI.logit.LDV.excl.corr$var)== "Lag_CIRI_PHYSINT"] <- "Past"




# pts renaming
names(PTS.logit.LDV.excl.corrfood$coefficients)[names(PTS.logit.LDV.excl.corrfood$coefficients) == "Lag_PTS_ai_reversed"] <- "Past"
rownames(PTS.logit.LDV.excl.corrfood$var)[rownames(PTS.logit.LDV.excl.corrfood$var)== "Lag_PTS_ai_reversed"] <- "Past"
colnames(PTS.logit.LDV.excl.corrfood$var)[colnames(PTS.logit.LDV.excl.corrfood$var)== "Lag_PTS_ai_reversed"] <- "Past"

names(PTS.logit.LDV.excl.corrchem$coefficients)[names(PTS.logit.LDV.excl.corrchem$coefficients) == "Lag_PTS_ai_reversed"] <- "Past"
rownames(PTS.logit.LDV.excl.corrchem$var)[rownames(PTS.logit.LDV.excl.corrchem$var)== "Lag_PTS_ai_reversed"] <- "Past"
colnames(PTS.logit.LDV.excl.corrchem$var)[colnames(PTS.logit.LDV.excl.corrchem$var)== "Lag_PTS_ai_reversed"] <- "Past"

names(PTS.logit.LDV.excl.corrmetal$coefficients)[names(PTS.logit.LDV.excl.corrmetal$coefficients) == "Lag_PTS_ai_reversed"] <- "Past"
rownames(PTS.logit.LDV.excl.corrmetal$var)[rownames(PTS.logit.LDV.excl.corrmetal$var)== "Lag_PTS_ai_reversed"] <- "Past"
colnames(PTS.logit.LDV.excl.corrmetal$var)[colnames(PTS.logit.LDV.excl.corrmetal$var)== "Lag_PTS_ai_reversed"] <- "Past"

names(PTS.logit.LDV.excl.corrmachin$coefficients)[names(PTS.logit.LDV.excl.corrmachin$coefficients) == "Lag_PTS_ai_reversed"] <- "Past"
rownames(PTS.logit.LDV.excl.corrmachin$var)[rownames(PTS.logit.LDV.excl.corrmachin$var)== "Lag_PTS_ai_reversed"] <- "Past"
colnames(PTS.logit.LDV.excl.corrmachin$var)[colnames(PTS.logit.LDV.excl.corrmachin$var)== "Lag_PTS_ai_reversed"] <- "Past"

names(PTS.logit.LDV.excl.correlect$coefficients)[names(PTS.logit.LDV.excl.correlect$coefficients) == "Lag_PTS_ai_reversed"] <- "Past"
rownames(PTS.logit.LDV.excl.correlect$var)[rownames(PTS.logit.LDV.excl.correlect$var)== "Lag_PTS_ai_reversed"] <- "Past"
colnames(PTS.logit.LDV.excl.correlect$var)[colnames(PTS.logit.LDV.excl.correlect$var)== "Lag_PTS_ai_reversed"] <- "Past"

names(PTS.logit.LDV.excl.corrtransp$coefficients)[names(PTS.logit.LDV.excl.corrtransp$coefficients) == "Lag_PTS_ai_reversed"] <- "Past"
rownames(PTS.logit.LDV.excl.corrtransp$var)[rownames(PTS.logit.LDV.excl.corrtransp$var)== "Lag_PTS_ai_reversed"] <- "Past"
colnames(PTS.logit.LDV.excl.corrtransp$var)[colnames(PTS.logit.LDV.excl.corrtransp$var)== "Lag_PTS_ai_reversed"] <- "Past"

names(PTS.logit.LDV.excl.corrwhotra $coefficients)[names(PTS.logit.LDV.excl.corrwhotra $coefficients) == "Lag_PTS_ai_reversed"] <- "Past"
rownames(PTS.logit.LDV.excl.corrwhotra $var)[rownames(PTS.logit.LDV.excl.corrwhotra $var)== "Lag_PTS_ai_reversed"] <- "Past"
colnames(PTS.logit.LDV.excl.corrwhotra $var)[colnames(PTS.logit.LDV.excl.corrwhotra $var)== "Lag_PTS_ai_reversed"] <- "Past"

names(PTS.logit.LDV.excl.corrdeposi$coefficients)[names(PTS.logit.LDV.excl.corrdeposi$coefficients) == "Lag_PTS_ai_reversed"] <- "Past"
rownames(PTS.logit.LDV.excl.corrdeposi$var)[rownames(PTS.logit.LDV.excl.corrdeposi$var)== "Lag_PTS_ai_reversed"] <- "Past"
colnames(PTS.logit.LDV.excl.corrdeposi$var)[colnames(PTS.logit.LDV.excl.corrdeposi$var)== "Lag_PTS_ai_reversed"] <- "Past"

names(PTS.logit.LDV.excl.corrpetrol$coefficients)[names(PTS.logit.LDV.excl.corrpetrol$coefficients) == "Lag_PTS_ai_reversed"] <- "Past"
rownames(PTS.logit.LDV.excl.corrpetrol$var)[rownames(PTS.logit.LDV.excl.corrpetrol$var)== "Lag_PTS_ai_reversed"] <- "Past"
colnames(PTS.logit.LDV.excl.corrpetrol$var)[colnames(PTS.logit.LDV.excl.corrpetrol$var)== "Lag_PTS_ai_reversed"] <- "Past"

names(PTS.logit.LDV.excl.corrmining$coefficients)[names(PTS.logit.LDV.excl.corrmining$coefficients) == "Lag_PTS_ai_reversed"] <- "Past"
rownames(PTS.logit.LDV.excl.corrmining$var)[rownames(PTS.logit.LDV.excl.corrmining$var)== "Lag_PTS_ai_reversed"] <- "Past"
colnames(PTS.logit.LDV.excl.corrmining$var)[colnames(PTS.logit.LDV.excl.corrmining$var)== "Lag_PTS_ai_reversed"] <- "Past"

names(PTS.logit.LDV.excl.corr$coefficients)[names(PTS.logit.LDV.excl.corr$coefficients) == "Lag_PTS_ai_reversed"] <- "Past"
rownames(PTS.logit.LDV.excl.corr$var)[rownames(PTS.logit.LDV.excl.corr$var)== "Lag_PTS_ai_reversed"] <- "Past"
colnames(PTS.logit.LDV.excl.corr$var)[colnames(PTS.logit.LDV.excl.corr$var)== "Lag_PTS_ai_reversed"] <- "Past"



#?fariss renaming
names(res.po.exclfood$coefficients)[names(res.po.exclfood$coefficients) == "Lag_Fariss_HR"] <- "Past"
rownames(res.po.exclfood$vcov)[rownames(res.po.exclfood$vcov)== "Lag_Fariss_HR"] <- "Past"
colnames(res.po.exclfood$vcov)[colnames(res.po.exclfood$vcov)== "Lag_Fariss_HR"] <- "Past"

names(res.po.exclchem$coefficients)[names(res.po.exclchem$coefficients) == "Lag_Fariss_HR"] <- "Past"
rownames(res.po.exclchem$vcov)[rownames(res.po.exclchem$vcov)== "Lag_Fariss_HR"] <- "Past"
colnames(res.po.exclchem$vcov)[colnames(res.po.exclchem$vcov)== "Lag_Fariss_HR"] <- "Past"

names(res.po.exclmetal$coefficients)[names(res.po.exclmetal$coefficients) == "Lag_Fariss_HR"] <- "Past"
rownames(res.po.exclmetal$vcov)[rownames(res.po.exclmetal$vcov)== "Lag_Fariss_HR"] <- "Past"
colnames(res.po.exclmetal$vcov)[colnames(res.po.exclmetal$vcov)== "Lag_Fariss_HR"] <- "Past"

names(res.po.exclmachin$coefficients)[names(res.po.exclmachin$coefficients) == "Lag_Fariss_HR"] <- "Past"
rownames(res.po.exclmachin$vcov)[rownames(res.po.exclmachin$vcov)== "Lag_Fariss_HR"] <- "Past"
colnames(res.po.exclmachin$vcov)[colnames(res.po.exclmachin$vcov)== "Lag_Fariss_HR"] <- "Past"

names(res.po.exclelect$coefficients)[names(res.po.exclelect$coefficients) == "Lag_Fariss_HR"] <- "Past"
rownames(res.po.exclelect$vcov)[rownames(res.po.exclelect$vcov)== "Lag_Fariss_HR"] <- "Past"
colnames(res.po.exclelect$vcov)[colnames(res.po.exclelect$vcov)== "Lag_Fariss_HR"] <- "Past"

names(res.po.excltransp$coefficients)[names(res.po.excltransp$coefficients) == "Lag_Fariss_HR"] <- "Past"
rownames(res.po.excltransp$vcov)[rownames(res.po.excltransp$vcov)== "Lag_Fariss_HR"] <- "Past"
colnames(res.po.excltransp$vcov)[colnames(res.po.excltransp$vcov)== "Lag_Fariss_HR"] <- "Past"

names(res.po.exclwhotra$coefficients)[names(res.po.exclwhotra$coefficients) == "Lag_Fariss_HR"] <- "Past"
rownames(res.po.exclwhotra$vcov)[rownames(res.po.exclwhotra$vcov)== "Lag_Fariss_HR"] <- "Past"
colnames(res.po.exclwhotra$vcov)[colnames(res.po.exclwhotra$vcov)== "Lag_Fariss_HR"] <- "Past"

names(res.po.excldeposi$coefficients)[names(res.po.excldeposi$coefficients) == "Lag_Fariss_HR"] <- "Past"
rownames(res.po.excldeposi$vcov)[rownames(res.po.excldeposi$vcov)== "Lag_Fariss_HR"] <- "Past"
colnames(res.po.excldeposi$vcov)[colnames(res.po.excldeposi$vcov)== "Lag_Fariss_HR"] <- "Past"

names(res.po.exclpetrol$coefficients)[names(res.po.exclpetrol$coefficients) == "Lag_Fariss_HR"] <- "Past"
rownames(res.po.exclpetrol$vcov)[rownames(res.po.exclpetrol$vcov)== "Lag_Fariss_HR"] <- "Past"
colnames(res.po.exclpetrol$vcov)[colnames(res.po.exclpetrol$vcov)== "Lag_Fariss_HR"] <- "Past"

names(res.po.exclmining$coefficients)[names(res.po.exclmining$coefficients) == "Lag_Fariss_HR"] <- "Past"
rownames(res.po.exclmining$vcov)[rownames(res.po.exclmining$vcov)== "Lag_Fariss_HR"] <- "Past"
colnames(res.po.exclmining$vcov)[colnames(res.po.exclmining$vcov)== "Lag_Fariss_HR"] <- "Past"

names(res.po.excl$coefficients)[names(res.po.excl$coefficients) == "Lag_Fariss_HR"] <- "Past"
rownames(res.po.excl$vcov)[rownames(res.po.excl$vcov)== "Lag_Fariss_HR"] <- "Past"
colnames(res.po.excl$vcov)[colnames(res.po.excl$vcov)== "Lag_Fariss_HR"] <- "Past"



#?build tables with stargazer CIRI
varnames <- c("Food FDI","Chemical FDI","Metal FDI","Machinery FDI","Electrical FDI","Transport FDI", "Wholesale FDI","Depository FDI", "Trade", "GDP p. capita", "Population", "Democracy", "Conflict","Employm. Services","Employm. Industry", "Past")
dvnames <- c("CIRI Integrity")
stargazer(CIRI.logit.LDV.excl.corrfood,CIRI.logit.LDV.excl.corrchem,CIRI.logit.LDV.excl.corrmetal,CIRI.logit.LDV.excl.corrmachin,CIRI.logit.LDV.excl.correlect,CIRI.logit.LDV.excl.corrtransp,CIRI.logit.LDV.excl.corrwhotra,CIRI.logit.LDV.excl.corrdeposi,CIRI.logit.LDV.excl.corr,
          covariate.labels=varnames, dep.var.labels=dvnames, 
          column.labels   = c("Sectoral FDI are employed separately in the models", "All"),
          column.separate = c(8, 1),
          omit=c("y>=1","y>=2","y>=3","y>=4","y>=5","y>=6","y>=7","y>=8","factor","Constant","(Intercept)"), 
          omit.stat = c("f","ser","chi2"),
          nobs=T,title="Sectoral FDI and CIRI Physical Integrity Rights, Sector Size Incl.  (sectors entered separately)",no.space=T,font.size="scriptsize", notes=c("Ordered logit with country-clustered standard errors. OLS with panel corrected standard errors.","All FDI is divided by GDP, then logged. GDP p. capita, population and trade are logged.","Past is the lagged dependent variable (LDV)."),notes.align="l",type="text",float.env="sidewaystable",label="Sectoral.separately.CIRI.size.tex",out="Sectoral.separately.CIRI.size.tex")

#?build tables with stargazer PTS Amnesty
varnames <- c("Food FDI","Chemical FDI","Metal FDI","Machinery FDI","Electrical FDI","Transport FDI", "Wholesale FDI","Depository FDI", "Trade", "GDP p. capita", "Population", "Democracy", "Conflict", "Employm. Services","Employm. Industry","Past")
dvnames <- c("PTS Amnesty")
stargazer(PTS.logit.LDV.excl.corrfood,PTS.logit.LDV.excl.corrchem,PTS.logit.LDV.excl.corrmetal,PTS.logit.LDV.excl.corrmachin,PTS.logit.LDV.excl.correlect,PTS.logit.LDV.excl.corrtransp,PTS.logit.LDV.excl.corrwhotra,PTS.logit.LDV.excl.corrdeposi,PTS.logit.LDV.excl.corr,
          covariate.labels=varnames, dep.var.labels=dvnames, 
          column.labels   = c("Sectoral FDI are employed separately in the models", "All"),
          column.separate = c(8, 1),
          omit=c("y>=1","y>=2","y>=3","y>=4","y>=5","y>=6","y>=7","y>=8","factor","Constant","(Intercept)"), 
          omit.stat = c("f","ser","chi2"),
          nobs=T,title="Sectoral FDI and PTS Amnesty Scores, Sector Size Incl. (sectors entered separately)",no.space=T,font.size="scriptsize", notes=c("Ordered logit with country-clustered standard errors. OLS with panel corrected standard errors.","All FDI is divided by GDP, then logged. GDP p. capita, population and trade are logged.","Past is the lagged dependent variable (LDV)."),notes.align="l",type="text",float.env="sidewaystable",label="Sectoral.separately.PTS.size.tex",out="Sectoral.separately.PTS.size.tex")

#?build tables with stargazer Latent Scores
varnames <- c("Food FDI","Chemical FDI","Metal FDI","Machinery FDI","Electrical FDI","Transport FDI", "Wholesale FDI","Depository FDI", "Trade", "GDP p. capita", "Population", "Democracy", "Conflict","Employm. Services","Employm. Industry", "Past")
dvnames <- c("Latent Scores")
stargazer(res.po.exclfood,res.po.exclchem, res.po.exclmetal, res.po.exclmachin, res.po.exclelect, res.po.excltransp, res.po.exclwhotra, res.po.excldeposi, res.po.excl,          
          covariate.labels=varnames, dep.var.labels=dvnames, 
          column.labels   = c("Sectoral FDI are employed separately in the models", "All"),
          column.separate = c(8, 1),
          omit=c("y>=1","y>=2","y>=3","y>=4","y>=5","y>=6","y>=7","y>=8","factor","Constant","(Intercept)"), 
          omit.stat = c("f","ser","chi2"),
          nobs=T,title="Sectoral FDI and Fariss Latent Scores, Sector Size Incl. (sectors entered separately)",no.space=T,font.size="scriptsize", notes=c("Ordered logit with country-clustered standard errors. OLS with panel corrected standard errors.","All FDI is divided by GDP, then logged. GDP p. capita, population and trade are logged.","Past is the lagged dependent variable (LDV)."),notes.align="l",type="text",float.env="sidewaystable",label="Sectoral.separately.Fariss.size.tex",out="Sectoral.separately.Fariss.size.tex")







##########################################################
# Table "Sample Size Analysis for Regression Models"
# (conducted in R, but values entered directly in Latex, so no stargazer was used)
##########################################################
library(pwr)
pwr.f2.test(u=14,v=NULL,f2=0.02,sig.level = 0.05, power = 0.8) #?how large must sample be for small effect detection
# 913.8

pwr.f2.test(u=14,v=NULL,f2=0.15,sig.level = 0.05, power = 0.8) # #?how large must sample be for medium effect detection
#?119.5


#Result: to detect small or medium results (f2=0.02 or 0.15, I need at least 119 (medium) or 913 (small) sample size. The T5 original model wiht all sectors in one for FARISS (935) meets these criteria. The models with CIRI (882) and PTS (886) are below that. The models with sector size measures in T5 (4-6) have sample sizes of 282-326. They would only be able to detect medium size effects but not small ones. Would that change if I remove 7 predictors and enter each sector separatel?

pwr.f2.test(u=7,v=NULL,f2=0.02,sig.level = 0.05, power = 0.8) #?# v = 716.5892
#?Answer: No, even if I remove 7 predictors, I could only predict medium effects, but not small ones with my sample size. 

pwr.f2.test(u=7,v=NULL,f2=0.15,sig.level = 0.05, power = 0.8) #?# v = 95

#Let's look at this the other way round, what is the power of my data sets in the two versions of entering all sectors at once, versus entering them one by one?





##########################################################
# Table "Power Analysis for Regression (Ideal Power at Least 0.8)"
# (conducted in R, but values entered directly in Latex, so no stargazer was used)
##########################################################

###############################################
#?WHAT IS THE POWER TO DETECT SMALL EFFECTS
###############################################


#?Baseline model with all sectors
pwr.f2.test(u=14,v=935,f2=0.02,sig.level = 0.05, power = NULL) #935 from Fariss
# power = 0.8114116 # power to detect small effects is close to desired 0.8 when having 14 predictors (all sectors at once)
pwr.f2.test(u=14,v=882,f2=0.02,sig.level = 0.05, power = NULL) #882 from CIRI
#    power = 0.7819068 power to detect small effects is still close but below desired 0.8 
pwr.f2.test(u=14,v=806,f2=0.02,sig.level = 0.05, power = NULL) #806 from PTS
#    power = power = 0.7338775 power to detect small effects is below desired 0.8 but not much; probabiltiy to detect effects is now 70% (still good)

# separete sector includsion model suggested by reviewer (T5, 1-3) with 7 predictors instead
pwr.f2.test(u=7,v=935,f2=0.02,sig.level = 0.05, power = NULL) #935 from Fariss
# power =  0.907846 # power to detect small effects is higher than desired 0.8 when having 7 predictors only
pwr.f2.test(u=7,v=882,f2=0.02,sig.level = 0.05, power = NULL) #882 from CIRI
#    power =  0.8877426 power to detect small effects 
pwr.f2.test(u=7,v=806,f2=0.02,sig.level = 0.05, power = NULL) #806 from PTS
#    power = 0.8525251 power to detect small effects is above desired 0.8 

# Power for T5, M1-3 is of course higher when only 7 predictors. For each outcome variable, when I reduce predictors from 14 to 7, power rises. For Fariss power is both times higher than0.8, so here the reduction predictors does not matter that much. For CIRI and PTS, the removal of 7 predictors moves power up to over .8 and so above the desired level, while with 14 predictors the power is .78 (CIRI) and .74 (PTS). Around the mark of .74 and higher I would not be massively concerned but of course higher power is always better. In general, if we wgith the potential of confounders as more important than the change in power, then the 14 are still fine.

pwr.f2.test(u=14,v=935,f2=0.02,sig.level = 0.05, power = NULL)
#0.8114116

pwr.f2.test(u=7,v=1526,f2=0.02,sig.level = 0.05, power = NULL) #lowest obs no in Fariss each sector separetly
# power = 0.9925908 
pwr.f2.test(u=7,v=2093,f2=0.02,sig.level = 0.05, power = NULL)
## power = 0.9995622


# now sector size included models
pwr.f2.test(u=16,v=326,f2=0.02,sig.level = 0.05, power = NULL)
# 0.2854822

pwr.f2.test(u=9,v=663,f2=0.02,sig.level = 0.05, power = NULL) #
# 0.7162413
pwr.f2.test(u=9,v=956,f2=0.02,sig.level = 0.05, power = NULL) #
# 0.8874215

pwr.f2.test(u=16,v=NULL,f2=0.02,sig.level = 0.05, power = 0.8) ## v = 959.6076
pwr.f2.test(u=16,v=NULL,f2=0.15,sig.level = 0.05, power = 0.8) ## v = 125.0842
pwr.f2.test(u=9,v=NULL,f2=0.02,sig.level = 0.05, power = 0.8) ## v = 780.9862
pwr.f2.test(u=9,v=NULL,f2=0.15,sig.level = 0.05, power = 0.8) ## v = 103.0567



###############################################
# WHAT IS THE POWER TO DETECT MEDIUM EFFECTS
###############################################


pwr.f2.test(u=14,v=935,f2=0.15,sig.level = 0.05, power = NULL)
#  power = 1

pwr.f2.test(u=7,v=1526,f2=0.15,sig.level = 0.05, power = NULL) #lowest obs no in Fariss each sector separetly
# power = 1
pwr.f2.test(u=7,v=2093,f2=0.15,sig.level = 0.05, power = NULL)
#   power = 1

#now sector size included models
pwr.f2.test(u=16,v=326,f2=0.15,sig.level = 0.05, power = NULL)
#  power = 0.99919451

pwr.f2.test(u=9,v=663,f2=0.15,sig.level = 0.05, power = NULL) #
# 1
pwr.f2.test(u=9,v=956,f2=0.15,sig.level = 0.05, power = NULL) #
# 0.8874215






# # # # # # # #  # # # #  # # # #  # # # #  # # # #  # # # #  # # # #  # # # #  # # # #  # # # #  # # # #  # # # #                
# additional power analysis with regard to country dummies where I have 118 predictors (the usual controls plus a dummy for each country) ####
# THIS REFERS TO FOOTNOTE STARTING: "Power analysis for multiple regression indicates that models including country dummies are least able..." in the main paper.
# # # #  # # # #  # # # #  # # # #  # # # #  # # # #  # # # #  # # # #  # # # #  # # # #  # # # #  # # # #  # # # #  

pwr.f2.test(u=118,v=NULL,f2=.15,sig.level = 0.05, power = 0.8)

# what effect size does my test have if I assume a conventional power of 0.8?
pwr.f2.test(u=14,v=935,f2=NULL,sig.level = 0.05, power = 0.8) 
# u = 14
# v = 935
# f2 = 0.01955116 #My study can detect very small effect sizes (close to Cohen's 0.02 cut off point) with the no of predictors and sample size I have.
# sig.level = 0.05
# power = 0.8

pwr.f2.test(u=19,v=935,f2=NULL,sig.level = 0.05, power = 0.8)
# u = 19
# v = 935
# f2 = 0.02186659 #My study can detect very small effect sizes (close to Cohen's 0.02 cut off point) with the no of predictors and sample size I have.
# sig.level = 0.05
# power = 0.8

pwr.f2.test(u=118,v=935,f2=NULL,sig.level = 0.05, power = 0.8)
# u = 118
# v = 935
# f2 = 0.04449095 ##My study can detect very small effect sizes (close to Cohen's 0.02 cut off point) with the no of predictors and sample size I have.
# sig.level = 0.05
# power = 0.8



# what sample size would I need?
pwr.f2.test(u=14,v=NULL,f2=0.15,sig.level = 0.05, power = 0.8)
# u = 14
# v = 119.5136 #I have 935
# f2 = 0.15
# sig.level = 0.05
# power = 0.8

pwr.f2.test(u=19,v=NULL,f2=0.15,sig.level = 0.05, power = 0.8)
# u = 19
# v = 132.6978 #I have 935
# f2 = 0.15
# sig.level = 0.05
# power = 0.8

pwr.f2.test(u=118,v=NULL,f2=0.15,sig.level = 0.05, power = 0.8)
# u = 118
# v = 250.6477 # #I have 935
# f2 = 0.15
# sig.level = 0.05
# power = 0.8



# what power does my test have?
pwr.f2.test(u=14,v=935,f2=0.15,sig.level = 0.05, power = NULL)
# u = 14
# v = 935
# f2 = 0.15
# sig.level = 0.05
# power = 1 # This means that I have practically no chance of a Type II error (chance of a 'miss', missing an effect)

pwr.f2.test(u=14,v=935,f2=0.35,sig.level = 0.05, power = NULL)
# u = 14
# v = 935
# f2 = 0.35
# sig.level = 0.05
# power = 1


pwr.f2.test(u=19,v=935,f2=0.15,sig.level = 0.05, power = NULL)
# u = 19
# v = 935
# f2 = 0.15
# sig.level = 0.05
# power = 1

pwr.f2.test(u=19,v=935,f2=0.02,sig.level = 0.05, power = NULL)
# u = 19
# v = 935
# f2 = 0.02
# sig.level = 0.05
# power = 0.7519567

pwr.f2.test(u=118,v=935,f2=0.15,sig.level = 0.05, power = NULL)
# u = 118
# v = 935
# f2 = 0.02
# sig.level = 0.05
# power = 0.9999996

pwr.f2.test(u=118,v=935,f2=0.02,sig.level = 0.05, power = NULL)
# u = 118
# v = 935
# f2 = 0.02
# sig.level = 0.05
# power = 0.3383101

# Result:
# All models have a sufficient sample size of 935 (105 countries over a varying amount of years, up to 28 years) to detect even small effect sizes. The power to detect a medium effect size of 0.15 is very high. The model with country dummies  (118 predictors) has good power to detect a medium sized effect, but it has little power to detect small effect sizes (Cohen's f2 = 0.02), leaving an about 70% chance of a type II error. Since the coefficients of hte sectoral FDI data are rather small across the models employed here, the country dummy model is least favourable.















###############################################
#Table "Sectoral FDI and Personal Integrity Rights Protection (1983-2010), including non-OECD and OECD countries"
###############################################
# Column 1-3
load("replicationdata.Rdata") #adjust to your working directory
tall <- replicationdata
droplevels(tall) # drop the unused levels (http://stackoverflow.com/questions/17217951/how-can-i-drop-unused-levels-from-a-data-frame)
dim(tall) #5568  38

#Data
data.CIRI.PHY.excl.LDV <- na.omit(tall[,cbind("CIRI_PHYSINT","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_foodpGDP","Lag_lognonnegUS_fdi_chemicalpGDP","Lag_lognonnegUS_fdi_prim_fab_metalpGDP","Lag_lognonnegUS_fdi_machinerypGDP","Lag_lognonnegUS_fdi_electricalpGDP","Lag_lognonnegUS_fdi_transportpGDP","Lag_lognonnegUS_fdi_whole_tradepGDP","Lag_lognonnegUS_fdi_depositorypGDP","Lag_CIRI_PHYSINT", "country","year")])

data.PTS.excl.LDV  <- na.omit(tall[,cbind("PTS_ai_reversed","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_foodpGDP","Lag_lognonnegUS_fdi_chemicalpGDP","Lag_lognonnegUS_fdi_prim_fab_metalpGDP","Lag_lognonnegUS_fdi_machinerypGDP","Lag_lognonnegUS_fdi_electricalpGDP","Lag_lognonnegUS_fdi_transportpGDP","Lag_lognonnegUS_fdi_whole_tradepGDP","Lag_lognonnegUS_fdi_depositorypGDP","Lag_PTS_ai_reversed","country","year")])

data.Fariss.excl.LDV <- na.omit(tall[,cbind("Fariss_HR","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_foodpGDP","Lag_lognonnegUS_fdi_chemicalpGDP","Lag_lognonnegUS_fdi_prim_fab_metalpGDP","Lag_lognonnegUS_fdi_machinerypGDP","Lag_lognonnegUS_fdi_electricalpGDP","Lag_lognonnegUS_fdi_transportpGDP","Lag_lognonnegUS_fdi_whole_tradepGDP","Lag_lognonnegUS_fdi_depositorypGDP","Lag_Fariss_HR","country","year")])
data.Fariss.excl.LDV <- as.data.frame(data.Fariss.excl.LDV)


#?Models
CIRI.logit.LDV.excl <- lrm(CIRI_PHYSINT ~ Lag_lognonnegUS_fdi_foodpGDP + Lag_lognonnegUS_fdi_chemicalpGDP + Lag_lognonnegUS_fdi_prim_fab_metalpGDP + Lag_lognonnegUS_fdi_machinerypGDP + Lag_lognonnegUS_fdi_electricalpGDP + Lag_lognonnegUS_fdi_transportpGDP + Lag_lognonnegUS_fdi_whole_tradepGDP + Lag_lognonnegUS_fdi_depositorypGDP+ Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_CIRI_PHYSINT,data=data.CIRI.PHY.excl.LDV  ,x=TRUE, y=TRUE)
CIRI.logit.LDV.excl.corr <- robcov(CIRI.logit.LDV.excl,data.CIRI.PHY.excl.LDV$country) #robust errors  


PTS.logit.LDV.excl <- lrm(PTS_ai_reversed ~ Lag_lognonnegUS_fdi_foodpGDP + Lag_lognonnegUS_fdi_chemicalpGDP + Lag_lognonnegUS_fdi_prim_fab_metalpGDP + Lag_lognonnegUS_fdi_machinerypGDP + Lag_lognonnegUS_fdi_electricalpGDP + Lag_lognonnegUS_fdi_transportpGDP + Lag_lognonnegUS_fdi_whole_tradepGDP + Lag_lognonnegUS_fdi_depositorypGDP+ Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_PTS_ai_reversed,data=data.PTS.excl.LDV,x=TRUE, y=TRUE)
PTS.logit.LDV.excl.corr <- robcov(PTS.logit.LDV.excl,data.PTS.excl.LDV$country) 

form   <- Fariss_HR ~  Lag_lognonnegUS_fdi_foodpGDP + Lag_lognonnegUS_fdi_chemicalpGDP + Lag_lognonnegUS_fdi_prim_fab_metalpGDP + Lag_lognonnegUS_fdi_machinerypGDP + Lag_lognonnegUS_fdi_electricalpGDP + Lag_lognonnegUS_fdi_transportpGDP + Lag_lognonnegUS_fdi_whole_tradepGDP + Lag_lognonnegUS_fdi_depositorypGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_Fariss_HR
res.po.excl <- plm(form,model="pooling",index=c("country","year"),data=data.Fariss.excl.LDV)
## PCSE a la Beck-Katz
res.BKtime.excl  <- vcovBK(x=res.po.excl,cluster="time")
ct.time.excl     <- coeftest(res.po.excl,vcov=res.BKtime.excl)
res.po.excl$vcov <- res.BKtime.excl  # replacing the first uncorrected residual covariance structure with the corrected SE
print(ct.time.excl)

names(CIRI.logit.LDV.excl.corr$coefficients)[names(CIRI.logit.LDV.excl.corr$coefficients) == "Lag_CIRI_PHYSINT"] <- "Past"
rownames(CIRI.logit.LDV.excl.corr$var)[rownames(CIRI.logit.LDV.excl.corr$var)== "Lag_CIRI_PHYSINT"] <- "Past"
colnames(CIRI.logit.LDV.excl.corr$var)[colnames(CIRI.logit.LDV.excl.corr$var)== "Lag_CIRI_PHYSINT"] <- "Past"

names(PTS.logit.LDV.excl.corr$coefficients)[names(PTS.logit.LDV.excl.corr$coefficients) == "Lag_PTS_ai_reversed"] <- "Past"
rownames(PTS.logit.LDV.excl.corr$var)[rownames(PTS.logit.LDV.excl.corr$var)== "Lag_PTS_ai_reversed"] <- "Past"
colnames(PTS.logit.LDV.excl.corr$var)[colnames(PTS.logit.LDV.excl.corr$var)== "Lag_PTS_ai_reversed"] <- "Past"


names(res.po.excl$coefficients)[names(res.po.excl$coefficients) == "Lag_Fariss_HR"] <- "Past"
rownames(res.po.excl$vcov)[rownames(res.po.excl$vcov)== "Lag_Fariss_HR"] <- "Past"
colnames(res.po.excl$vcov)[colnames(res.po.excl$vcov)== "Lag_Fariss_HR"] <- "Past"


# save the data sets (with all sectors included on all countries) for reuse later if necessary
CIRI.logit.LDV.excl.corr.baseline.allctr <- CIRI.logit.LDV.excl.corr
PTS.logit.LDV.excl.corr.baseline.allctr <- PTS.logit.LDV.excl.corr
res.po.excl.baseline.allctr <- res.po.excl


#Column 4-6
#including sector size "Lag_log_Employment_Services", "Lag_log_Employment_Industry"


#Data incl "Lag_log_Employment_Services", "Lag_log_Employment_Industry"
data.CIRI.PHY.excl.LDV <- na.omit(tall[,cbind("CIRI_PHYSINT","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_foodpGDP","Lag_lognonnegUS_fdi_chemicalpGDP","Lag_lognonnegUS_fdi_prim_fab_metalpGDP","Lag_lognonnegUS_fdi_machinerypGDP","Lag_lognonnegUS_fdi_electricalpGDP","Lag_lognonnegUS_fdi_transportpGDP","Lag_lognonnegUS_fdi_whole_tradepGDP","Lag_lognonnegUS_fdi_depositorypGDP","Lag_CIRI_PHYSINT", "country","year","Lag_log_Employment_Services", "Lag_log_Employment_Industry")])

data.PTS.excl.LDV  <- na.omit(tall[,cbind("PTS_ai_reversed","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_foodpGDP","Lag_lognonnegUS_fdi_chemicalpGDP","Lag_lognonnegUS_fdi_prim_fab_metalpGDP","Lag_lognonnegUS_fdi_machinerypGDP","Lag_lognonnegUS_fdi_electricalpGDP","Lag_lognonnegUS_fdi_transportpGDP","Lag_lognonnegUS_fdi_whole_tradepGDP","Lag_lognonnegUS_fdi_depositorypGDP","Lag_PTS_ai_reversed","country","year","Lag_log_Employment_Services", "Lag_log_Employment_Industry")])

data.Fariss.excl.LDV <- na.omit(tall[,cbind("Fariss_HR","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_foodpGDP","Lag_lognonnegUS_fdi_chemicalpGDP","Lag_lognonnegUS_fdi_prim_fab_metalpGDP","Lag_lognonnegUS_fdi_machinerypGDP","Lag_lognonnegUS_fdi_electricalpGDP","Lag_lognonnegUS_fdi_transportpGDP","Lag_lognonnegUS_fdi_whole_tradepGDP","Lag_lognonnegUS_fdi_depositorypGDP","Lag_Fariss_HR","country","year","Lag_log_Employment_Services", "Lag_log_Employment_Industry")])
data.Fariss.excl.LDV <- as.data.frame(data.Fariss.excl.LDV)

# MODELS ####
CIRI.logit.LDV.excl <- lrm(CIRI_PHYSINT ~ Lag_lognonnegUS_fdi_foodpGDP + Lag_lognonnegUS_fdi_chemicalpGDP + Lag_lognonnegUS_fdi_prim_fab_metalpGDP + Lag_lognonnegUS_fdi_machinerypGDP + Lag_lognonnegUS_fdi_electricalpGDP + Lag_lognonnegUS_fdi_transportpGDP + Lag_lognonnegUS_fdi_whole_tradepGDP + Lag_lognonnegUS_fdi_depositorypGDP+ Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_log_Employment_Services + Lag_log_Employment_Industry+ Lag_CIRI_PHYSINT,data=data.CIRI.PHY.excl.LDV  ,x=TRUE, y=TRUE)
CIRI.logit.LDV.excl.corr <- robcov(CIRI.logit.LDV.excl,data.CIRI.PHY.excl.LDV$country) #robust errors  



PTS.logit.LDV.excl <- lrm(PTS_ai_reversed ~ Lag_lognonnegUS_fdi_foodpGDP + Lag_lognonnegUS_fdi_chemicalpGDP + Lag_lognonnegUS_fdi_prim_fab_metalpGDP + Lag_lognonnegUS_fdi_machinerypGDP + Lag_lognonnegUS_fdi_electricalpGDP + Lag_lognonnegUS_fdi_transportpGDP + Lag_lognonnegUS_fdi_whole_tradepGDP + Lag_lognonnegUS_fdi_depositorypGDP+ Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_log_Employment_Services + Lag_log_Employment_Industry+ Lag_PTS_ai_reversed,data=data.PTS.excl.LDV,x=TRUE, y=TRUE)
PTS.logit.LDV.excl.corr <- robcov(PTS.logit.LDV.excl,data.PTS.excl.LDV$country) 


form   <- Fariss_HR ~  Lag_lognonnegUS_fdi_foodpGDP + Lag_lognonnegUS_fdi_chemicalpGDP + Lag_lognonnegUS_fdi_prim_fab_metalpGDP + Lag_lognonnegUS_fdi_machinerypGDP + Lag_lognonnegUS_fdi_electricalpGDP + Lag_lognonnegUS_fdi_transportpGDP + Lag_lognonnegUS_fdi_whole_tradepGDP + Lag_lognonnegUS_fdi_depositorypGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_log_Employment_Services + Lag_log_Employment_Industry+ Lag_Fariss_HR
res.po.excl <- plm(form,model="pooling",index=c("country","year"),data=data.Fariss.excl.LDV)
## PCSE a la Beck-Katz
res.BKtime.excl  <- vcovBK(x=res.po.excl,cluster="time")
ct.time.excl     <- coeftest(res.po.excl,vcov=res.BKtime.excl)
res.po.excl$vcov <- res.BKtime.excl  # replacing the first uncorrected residual covariance structure with the corrected SE
print(ct.time.excl)


# Create three tables (CIRI, PTS, Fariss) with these results, last column is what T5 used to be (all sectors in one exclusive petrol and mining)

names(CIRI.logit.LDV.excl.corr$coefficients)[names(CIRI.logit.LDV.excl.corr$coefficients) == "Lag_CIRI_PHYSINT"] <- "Past"
rownames(CIRI.logit.LDV.excl.corr$var)[rownames(CIRI.logit.LDV.excl.corr$var)== "Lag_CIRI_PHYSINT"] <- "Past"
colnames(CIRI.logit.LDV.excl.corr$var)[colnames(CIRI.logit.LDV.excl.corr$var)== "Lag_CIRI_PHYSINT"] <- "Past"

names(PTS.logit.LDV.excl.corr$coefficients)[names(PTS.logit.LDV.excl.corr$coefficients) == "Lag_PTS_ai_reversed"] <- "Past"
rownames(PTS.logit.LDV.excl.corr$var)[rownames(PTS.logit.LDV.excl.corr$var)== "Lag_PTS_ai_reversed"] <- "Past"
colnames(PTS.logit.LDV.excl.corr$var)[colnames(PTS.logit.LDV.excl.corr$var)== "Lag_PTS_ai_reversed"] <- "Past"

names(res.po.excl$coefficients)[names(res.po.excl$coefficients) == "Lag_Fariss_HR"] <- "Past"
rownames(res.po.excl$vcov)[rownames(res.po.excl$vcov)== "Lag_Fariss_HR"] <- "Past"
colnames(res.po.excl$vcov)[colnames(res.po.excl$vcov)== "Lag_Fariss_HR"] <- "Past"

# save models with all sectors included and all countires and sector size to correspond to T5 in main text
CIRI.logit.LDV.excl.corr.secsize.allctr <- CIRI.logit.LDV.excl.corr
PTS.logit.LDV.excl.corr.secsize.allctr <- PTS.logit.LDV.excl.corr
res.po.excl.secsize.allctr <- res.po.excl

#?Table corresponding to T5 in main text: but now on all countries instead of non-OECD only
#?large table with all sectoral models
varnames <- c("Food FDI","Chemical FDI","Metal FDI","Machinery FDI","Electrical FDI","Transport FDI", "Wholesale FDI","Depository FDI", "Trade", "GDP p. capita", "Population", "Democracy", "Conflict","Employm. Services","Employm. Industry", "Past")
dvnames <- c("CIRI Integrity","PTS Amnesty","Latent Scores","CIRI Integrity","PTS Amnesty","Latent Scores")
stargazer(CIRI.logit.LDV.excl.corr.baseline.allctr, 
          PTS.logit.LDV.excl.corr.baseline.allctr,
          res.po.excl.baseline.allctr,
          CIRI.logit.LDV.excl.corr.secsize.allctr,
          PTS.logit.LDV.excl.corr.secsize.allctr,
          res.po.excl.secsize.allctr, 
          covariate.labels=varnames, dep.var.labels=dvnames, 
          column.labels   = c("Baseline", "Sector Size Included"),
          column.separate = c(3, 3),
          omit=c("y>=1","y>=2","y>=3","y>=4","y>=5","y>=6","y>=7","y>=8","factor","Constant","(Intercept)"), 
          omit.stat = c("f","ser","chi2"),
          nobs=T,title="Sectoral FDI and Personal Integrity Rights Protection (1983-2010), including non-OECD and OECD countries",no.space=T,font.size="scriptsize", notes=c("Ordered logit with country-clustered standard errors. OLS with panel corrected standard errors.","All FDI is divided by GDP, then logged. GDP p. capita, population and trade are logged.","Past is the lagged dependent variable (LDV)."),notes.align="l",type="text",label="Sectoral.base.size.allctr.tex",out="Sectoral.base.size.allctr.tex")



















# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 
# More lags between FDI and Human Rights
# "Total U.S. FDI and Personal Integrity Rights, 2-5 year lags"
# "Sectoral FDI and Personal Integrity Rights, 2-5 year lags"
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 

# I will first create data sets t.nono.2, t.nono.3 ... etc and in each data set the lag between the outcome and FDI variables increases. If you now see a variable Lag_ ... in the models, it refers to 2,3,4,5 year lags respectively that were introduced step by step.

# adding 2-5 year lags between FDI and human rights protection

dim(t.nono) #4582  38
tobelagged <- c("lognonnegUS_fdi_totalpGDP","lognonnegUS_fdi_petrolpGDP","lognonnegUS_fdi_foodpGDP","lognonnegUS_fdi_chemicalpGDP","lognonnegUS_fdi_prim_fab_metalpGDP","lognonnegUS_fdi_machinerypGDP","lognonnegUS_fdi_electricalpGDP","lognonnegUS_fdi_transportpGDP", "lognonnegUS_fdi_whole_tradepGDP","lognonnegUS_fdi_depositorypGDP","lognonnegUS_fdi_miningpGDP") #these are the original logged FDI/GDP variables without any lag

# datasets with same names of all variables (easier for coding)

# 2 year lag
# adjust lagging function
my.lag.vars <- function(x,vars,prefix="Lag_"){
  
  if (!all(vars%in%colnames(x))) stop("some vars are not in colnames(x)")
  for (i in vars){
    newname <- paste(prefix,i,sep="")
    x[newname] <- lagpanel(x[i],x$country,x$year,2) #we don't need cbind since we already create new variables with new name
  } #end for
  
  return(x)
} 
d2 <- my.lag.vars(t.nono,vars=tobelagged)
d2[c(200:240),c(241,317)] #did it work? yes!
t.nono.2 <- d2

# 3 year lag
# adjust lagging function
my.lag.vars <- function(x,vars,prefix="Lag_"){
  
  if (!all(vars%in%colnames(x))) stop("some vars are not in colnames(x)")
  for (i in vars){
    newname <- paste(prefix,i,sep="")
    x[newname] <- lagpanel(x[i],x$country,x$year,3) #we don't need cbind since we already create new variables with new name
  } #end for
  
  return(x)
} 
d2 <- my.lag.vars(t.nono,vars=tobelagged)
d2[c(200:240),c(241,317)] #did it work? yes!
t.nono.3 <- d2


# 4 year lag
my.lag.vars <- function(x,vars,prefix="Lag_"){
  
  if (!all(vars%in%colnames(x))) stop("some vars are not in colnames(x)")
  for (i in vars){
    newname <- paste(prefix,i,sep="")
    x[newname] <- lagpanel(x[i],x$country,x$year,4) #we don't need cbind since we already create new variables with new name
  } #end for
  
  return(x)
} 
d2 <- my.lag.vars(t.nono,vars=tobelagged)
d2[c(200:240),c(241,317)] #did it work? yes!
t.nono.4 <- d2


# 5 year lag
my.lag.vars <- function(x,vars,prefix="Lag_"){
  
  if (!all(vars%in%colnames(x))) stop("some vars are not in colnames(x)")
  for (i in vars){
    newname <- paste(prefix,i,sep="")
    x[newname] <- lagpanel(x[i],x$country,x$year,5) #we don't need cbind since we already create new variables with new name
  } #end for
  
  return(x)
} 
d2 <- my.lag.vars(t.nono,vars=tobelagged)
d2[c(200:240),c(241,317)] #did it work? yes!
t.nono.5 <- d2

# We now have nearly identical data sets, except that each introduces a longer lag between FDI and outcome variable (e.g. t.nono.5 produces a 5-year lag)


# Models for 2 year lag####
t.nono <- t.nono.2 #replacing t.nono with the 2-year-lag version; this way I can re-use all code bits below

# data####
#US total FDI
data.CIRI.PHY.LDV <- na.omit(t.nono[,cbind("CIRI_PHYSINT","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_totalpGDP","Lag_CIRI_PHYSINT", "country","year")])
data.PTS.LDV <- na.omit(t.nono[,cbind("PTS_ai_reversed","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_totalpGDP", "Lag_PTS_ai_reversed", "country","year")])
data.Fariss.LDV <- na.omit(t.nono[,cbind("Fariss_HR","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_totalpGDP", "Lag_Fariss_HR","country","year")]) 
data.Fariss.LDV$country <- factor(as.character(data.Fariss.LDV$country)) 

# UN total FDI
data.CIRI.PHY1.LDV <- na.omit(t.nono[,cbind("CIRI_PHYSINT","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_log_UN_FDI_stock_pgdp" ,"Lag_CIRI_PHYSINT","country","year")])
data.PTS1.LDV <- na.omit(t.nono[,cbind("PTS_ai_reversed","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_log_UN_FDI_stock_pgdp","Lag_PTS_ai_reversed", "country","year")])
data.Fariss1.LDV <- na.omit(t.nono[,cbind("Fariss_HR","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_log_UN_FDI_stock_pgdp", "Lag_Fariss_HR","country","year")])

# data for sectoral models
data.CIRI.PHY.excl.LDV <- na.omit(t.nono[,cbind("CIRI_PHYSINT","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_foodpGDP","Lag_lognonnegUS_fdi_chemicalpGDP","Lag_lognonnegUS_fdi_prim_fab_metalpGDP","Lag_lognonnegUS_fdi_machinerypGDP","Lag_lognonnegUS_fdi_electricalpGDP","Lag_lognonnegUS_fdi_transportpGDP","Lag_lognonnegUS_fdi_whole_tradepGDP","Lag_lognonnegUS_fdi_depositorypGDP","Lag_CIRI_PHYSINT", "country","year")])
data.PTS.excl.LDV  <- na.omit(t.nono[,cbind("PTS_ai_reversed","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_foodpGDP","Lag_lognonnegUS_fdi_chemicalpGDP","Lag_lognonnegUS_fdi_prim_fab_metalpGDP","Lag_lognonnegUS_fdi_machinerypGDP","Lag_lognonnegUS_fdi_electricalpGDP","Lag_lognonnegUS_fdi_transportpGDP","Lag_lognonnegUS_fdi_whole_tradepGDP","Lag_lognonnegUS_fdi_depositorypGDP","Lag_PTS_ai_reversed","country","year")])
data.Fariss.excl.LDV <- na.omit(t.nono[,cbind("Fariss_HR","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_foodpGDP","Lag_lognonnegUS_fdi_chemicalpGDP","Lag_lognonnegUS_fdi_prim_fab_metalpGDP","Lag_lognonnegUS_fdi_machinerypGDP","Lag_lognonnegUS_fdi_electricalpGDP","Lag_lognonnegUS_fdi_transportpGDP","Lag_lognonnegUS_fdi_whole_tradepGDP","Lag_lognonnegUS_fdi_depositorypGDP","Lag_Fariss_HR","country","year")])

# models 2 year lag####
CIRI.logit.LDV <- lrm(CIRI_PHYSINT ~ Lag_lognonnegUS_fdi_totalpGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_CIRI_PHYSINT,data=data.CIRI.PHY.LDV,x=TRUE, y=TRUE)
CIRI.logit.LDV.corr <- robcov(CIRI.logit.LDV,data.CIRI.PHY.LDV$country) #robust errors 

PTS.logit.LDV <- lrm(PTS_ai_reversed ~ Lag_lognonnegUS_fdi_totalpGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_PTS_ai_reversed,data=data.PTS.LDV,x=TRUE, y=TRUE)
PTS.logit.LDV.corr <- robcov(PTS.logit.LDV,data.PTS.LDV$country) #robust errors 

#Fariss xtpcse with LDV
data.Fariss.LDV <- as.data.frame(data.Fariss.LDV)
form   <- Fariss_HR ~ Lag_lognonnegUS_fdi_totalpGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_Fariss_HR
res.po <- plm(form,model="pooling",index=c("country","year"),data=data.Fariss.LDV)
summary(res.po)      
## PCSE a la Beck-Katz
res.BKtime  <- vcovBK(x=res.po,cluster="time")
ct.time     <- coeftest(res.po,vcov=res.BKtime)
print(ct.time)

# rename lagged values to get them into one row with stargazer; replace plm se's with corrected ones
names(CIRI.logit.LDV.corr$coefficients)
names(CIRI.logit.LDV.corr$coefficients)[names(CIRI.logit.LDV.corr$coefficients) == "Lag_CIRI_PHYSINT"] <- "Past"
rownames(CIRI.logit.LDV.corr$var)[rownames(CIRI.logit.LDV.corr$var)== "Lag_CIRI_PHYSINT"] <- "Past"
colnames(CIRI.logit.LDV.corr$var)[colnames(CIRI.logit.LDV.corr$var)== "Lag_CIRI_PHYSINT"] <- "Past"

names(PTS.logit.LDV.corr$coefficients)
names(PTS.logit.LDV.corr$coefficients)[names(PTS.logit.LDV.corr$coefficients) == "Lag_PTS_ai_reversed"] <- "Past"
rownames(PTS.logit.LDV.corr$var)[rownames(PTS.logit.LDV.corr$var)== "Lag_PTS_ai_reversed"] <- "Past"
colnames(PTS.logit.LDV.corr$var)[colnames(PTS.logit.LDV.corr$var)== "Lag_PTS_ai_reversed"] <- "Past"

res.po$vcov <- res.BKtime  # replacing the first uncorrected residual covariance structure with the corrected one see str()
names(res.po$coefficients)[names(res.po$coefficients) == "Lag_Fariss_HR"] <- "Past"
rownames(res.po$vcov)[rownames(res.po$vcov)== "Lag_Fariss_HR"] <- "Past"
colnames(res.po$vcov)[colnames(res.po$vcov)== "Lag_Fariss_HR"] <- "Past"

# Results Total UN FDI 
CIRI.logit.LDV.UN <- lrm(CIRI_PHYSINT ~ Lag_log_UN_FDI_stock_pgdp + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_CIRI_PHYSINT,data=data.CIRI.PHY1.LDV ,x=TRUE, y=TRUE)
CIRI.logit.LDV.UN.corr <- robcov(CIRI.logit.LDV.UN,data.CIRI.PHY1.LDV$country) #robust errors  

PTS.logit.LDV.UN <- lrm(PTS_ai_reversed ~ Lag_log_UN_FDI_stock_pgdp + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_PTS_ai_reversed,data=data.PTS1.LDV,x=TRUE, y=TRUE)
PTS.logit.LDV.UN.corr <- robcov(PTS.logit.LDV.UN,data.PTS1.LDV$country) #robust errors  

data.Fariss.1.LDV <- as.data.frame(data.Fariss1.LDV)
form   <- Fariss_HR ~ Lag_log_UN_FDI_stock_pgdp + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_Fariss_HR
res.po1 <- plm(form,model="pooling",index=c("country","year"),data=data.Fariss1.LDV)
summary(res.po1)      
## PCSE a la Beck-Katz
res.BKtime1  <- vcovBK(x=res.po1,cluster="time")
ct.time1     <- coeftest(res.po1,vcov=res.BKtime1)
print(ct.time1) # 

# rename lagged values to get them into one row
names(CIRI.logit.LDV.UN.corr$coefficients)
names(CIRI.logit.LDV.UN.corr$coefficients)[names(CIRI.logit.LDV.UN.corr$coefficients) == "Lag_CIRI_PHYSINT"] <- "Past"
rownames(CIRI.logit.LDV.UN.corr$var)[rownames(CIRI.logit.LDV.UN.corr$var)== "Lag_CIRI_PHYSINT"] <- "Past"
colnames(CIRI.logit.LDV.UN.corr$var)[colnames(CIRI.logit.LDV.UN.corr$var)== "Lag_CIRI_PHYSINT"] <- "Past"
names(PTS.logit.LDV.UN.corr$coefficients)
names(PTS.logit.LDV.UN.corr$coefficients)[names(PTS.logit.LDV.UN.corr$coefficients) == "Lag_PTS_ai_reversed"] <- "Past"
rownames(PTS.logit.LDV.UN.corr$var)[rownames(PTS.logit.LDV.UN.corr$var)== "Lag_PTS_ai_reversed"] <- "Past"
colnames(PTS.logit.LDV.UN.corr$var)[colnames(PTS.logit.LDV.UN.corr$var)== "Lag_PTS_ai_reversed"] <- "Past"

res.po1$vcov <- res.BKtime1  # replacing the first uncorrected residual covariance structure with the corrected one see str()
names(res.po1$coefficients)[names(res.po1$coefficients) == "Lag_Fariss_HR"] <- "Past"
rownames(res.po1$vcov)[rownames(res.po1$vcov)== "Lag_Fariss_HR"] <- "Past"
colnames(res.po1$vcov)[colnames(res.po1$vcov)== "Lag_Fariss_HR"] <- "Past"

# sectoral (baseline)####
CIRI.logit.LDV.excl <- lrm(CIRI_PHYSINT ~ Lag_lognonnegUS_fdi_foodpGDP + Lag_lognonnegUS_fdi_chemicalpGDP + Lag_lognonnegUS_fdi_prim_fab_metalpGDP + Lag_lognonnegUS_fdi_machinerypGDP + Lag_lognonnegUS_fdi_electricalpGDP + Lag_lognonnegUS_fdi_transportpGDP + Lag_lognonnegUS_fdi_whole_tradepGDP + Lag_lognonnegUS_fdi_depositorypGDP+ Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_CIRI_PHYSINT,data=data.CIRI.PHY.excl.LDV  ,x=TRUE, y=TRUE)
CIRI.logit.LDV.excl.corr <- robcov(CIRI.logit.LDV.excl,data.CIRI.PHY.excl.LDV$country) #robust errors  

PTS.logit.LDV.excl <- lrm(PTS_ai_reversed ~ Lag_lognonnegUS_fdi_foodpGDP + Lag_lognonnegUS_fdi_chemicalpGDP + Lag_lognonnegUS_fdi_prim_fab_metalpGDP + Lag_lognonnegUS_fdi_machinerypGDP + Lag_lognonnegUS_fdi_electricalpGDP + Lag_lognonnegUS_fdi_transportpGDP + Lag_lognonnegUS_fdi_whole_tradepGDP + Lag_lognonnegUS_fdi_depositorypGDP+ Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_PTS_ai_reversed,data=data.PTS.excl.LDV,x=TRUE, y=TRUE)
PTS.logit.LDV.excl.corr <- robcov(PTS.logit.LDV.excl,data.PTS.excl.LDV$country) 

data.Fariss.excl.LDV <- as.data.frame(data.Fariss.excl.LDV)
form   <- Fariss_HR ~  Lag_lognonnegUS_fdi_foodpGDP + Lag_lognonnegUS_fdi_chemicalpGDP + Lag_lognonnegUS_fdi_prim_fab_metalpGDP + Lag_lognonnegUS_fdi_machinerypGDP + Lag_lognonnegUS_fdi_electricalpGDP + Lag_lognonnegUS_fdi_transportpGDP + Lag_lognonnegUS_fdi_whole_tradepGDP + Lag_lognonnegUS_fdi_depositorypGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_Fariss_HR
res.po.excl <- plm(form,model="pooling",index=c("country","year"),data=data.Fariss.excl.LDV)
summary(res.po.excl)      
## PCSE a la Beck-Katz
res.BKtime.excl  <- vcovBK(x=res.po.excl,cluster="time")
ct.time.excl     <- coeftest(res.po.excl,vcov=res.BKtime.excl)
print(ct.time.excl)

# rename lagged values to get them into one row
names(CIRI.logit.LDV.excl.corr$coefficients)
names(CIRI.logit.LDV.excl.corr$coefficients)[names(CIRI.logit.LDV.excl.corr$coefficients) == "Lag_CIRI_PHYSINT"] <- "Past"
rownames(CIRI.logit.LDV.excl.corr$var)[rownames(CIRI.logit.LDV.excl.corr$var)== "Lag_CIRI_PHYSINT"] <- "Past"
colnames(CIRI.logit.LDV.excl.corr$var)[colnames(CIRI.logit.LDV.excl.corr$var)== "Lag_CIRI_PHYSINT"] <- "Past"
names(PTS.logit.LDV.excl.corr$coefficients)
names(PTS.logit.LDV.excl.corr$coefficients)[names(PTS.logit.LDV.excl.corr$coefficients) == "Lag_PTS_ai_reversed"] <- "Past"
rownames(PTS.logit.LDV.excl.corr$var)[rownames(PTS.logit.LDV.excl.corr$var)== "Lag_PTS_ai_reversed"] <- "Past"
colnames(PTS.logit.LDV.excl.corr$var)[colnames(PTS.logit.LDV.excl.corr$var)== "Lag_PTS_ai_reversed"] <- "Past"


res.po.excl$vcov <- res.BKtime.excl  # replacing the first uncorrected residual covariance structure with the corrected one see str()
names(res.po.excl$coefficients)[names(res.po.excl$coefficients) == "Lag_Fariss_HR"] <- "Past"
rownames(res.po.excl$vcov)[rownames(res.po.excl$vcov)== "Lag_Fariss_HR"] <- "Past"
colnames(res.po.excl$vcov)[colnames(res.po.excl$vcov)== "Lag_Fariss_HR"] <- "Past"

varnames <- c("Food FDI","Chemical FDI","Metal FDI","Machinery FDI","Electrical FDI","Transport FDI", "Whole. Trade FDI","Depository FDI", "Trade", "GDP p. capita", "Population", "Democracy", "Conflict", "Past")
dvnames <- c("CIRI Integrity","PTS Amnesty","Latent Scores")
stargazer(CIRI.logit.LDV.excl.corr,PTS.logit.LDV.excl.corr,res.po.excl, 
          covariate.labels=varnames, dep.var.labels=dvnames, 
          omit=c("y>=1","y>=2","y>=3","y>=4","y>=5","y>=6","y>=7","y>=8","factor","Constant","(Intercept)"), 
          omit.stat = c("f","ser","chi2"),
          nobs=T,title="Sectoral FDI (without resources) and Repression [2 year lag]",no.space=T,font.size="scriptsize", notes=c("Ordered logit with country-clustered standard errors.","OLS with panel corrected standard errors.","Ordered Logit Models: Nagelkerke R-squared.","FDIpGDP, GDPpc, population, trade are logged.","Past is the lagged dependent variable (LDV).","Years included: 1984-2010."),notes.align="l",type="text",label="Exclusive.2yearlag.tex",out="2yearlag.tex")


# ending 2 year lag with saving res objects####
CIRI.logit.LDV.UN.corr.2 <- CIRI.logit.LDV.UN.corr
PTS.logit.LDV.UN.corr.2 <- PTS.logit.LDV.UN.corr
res.po1.2 <- res.po1
CIRI.logit.LDV.corr.2 <- CIRI.logit.LDV.corr
PTS.logit.LDV.corr.2 <- PTS.logit.LDV.corr
res.po.2 <- res.po
CIRI.logit.LDV.excl.corr.2 <- CIRI.logit.LDV.excl.corr
PTS.logit.LDV.excl.corr.2 <- PTS.logit.LDV.excl.corr
res.po.excl.2 <- res.po.excl








#?Main Models for 3 year lag####
t.nono <- t.nono.3

# data####
#US
data.CIRI.PHY.LDV <- na.omit(t.nono[,cbind("CIRI_PHYSINT","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_totalpGDP","Lag_CIRI_PHYSINT", "country","year")])
data.PTS.LDV <- na.omit(t.nono[,cbind("PTS_ai_reversed","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_totalpGDP", "Lag_PTS_ai_reversed", "country","year")])
data.Fariss.LDV <- na.omit(t.nono[,cbind("Fariss_HR","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_totalpGDP", "Lag_Fariss_HR","country","year")]) 
data.Fariss.LDV$country <- factor(as.character(data.Fariss.LDV$country)) 
# UN
data.CIRI.PHY1.LDV <- na.omit(t.nono[,cbind("CIRI_PHYSINT","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_log_UN_FDI_stock_pgdp" ,"Lag_CIRI_PHYSINT","country","year")])
data.PTS1.LDV <- na.omit(t.nono[,cbind("PTS_ai_reversed","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_log_UN_FDI_stock_pgdp","Lag_PTS_ai_reversed", "country","year")])
data.Fariss1.LDV <- na.omit(t.nono[,cbind("Fariss_HR","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_log_UN_FDI_stock_pgdp", "Lag_Fariss_HR","country","year")])
# data for SECTORAL
data.CIRI.PHY.excl.LDV <- na.omit(t.nono[,cbind("CIRI_PHYSINT","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_foodpGDP","Lag_lognonnegUS_fdi_chemicalpGDP","Lag_lognonnegUS_fdi_prim_fab_metalpGDP","Lag_lognonnegUS_fdi_machinerypGDP","Lag_lognonnegUS_fdi_electricalpGDP","Lag_lognonnegUS_fdi_transportpGDP","Lag_lognonnegUS_fdi_whole_tradepGDP","Lag_lognonnegUS_fdi_depositorypGDP","Lag_CIRI_PHYSINT", "country","year")])
data.PTS.excl.LDV  <- na.omit(t.nono[,cbind("PTS_ai_reversed","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_foodpGDP","Lag_lognonnegUS_fdi_chemicalpGDP","Lag_lognonnegUS_fdi_prim_fab_metalpGDP","Lag_lognonnegUS_fdi_machinerypGDP","Lag_lognonnegUS_fdi_electricalpGDP","Lag_lognonnegUS_fdi_transportpGDP","Lag_lognonnegUS_fdi_whole_tradepGDP","Lag_lognonnegUS_fdi_depositorypGDP","Lag_PTS_ai_reversed","country","year")])
data.Fariss.excl.LDV <- na.omit(t.nono[,cbind("Fariss_HR","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_foodpGDP","Lag_lognonnegUS_fdi_chemicalpGDP","Lag_lognonnegUS_fdi_prim_fab_metalpGDP","Lag_lognonnegUS_fdi_machinerypGDP","Lag_lognonnegUS_fdi_electricalpGDP","Lag_lognonnegUS_fdi_transportpGDP","Lag_lognonnegUS_fdi_whole_tradepGDP","Lag_lognonnegUS_fdi_depositorypGDP","Lag_Fariss_HR","country","year")])

# models 3 year lag####
CIRI.logit.LDV <- lrm(CIRI_PHYSINT ~ Lag_lognonnegUS_fdi_totalpGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_CIRI_PHYSINT,data=data.CIRI.PHY.LDV,x=TRUE, y=TRUE)
CIRI.logit.LDV.corr <- robcov(CIRI.logit.LDV,data.CIRI.PHY.LDV$country) #robust errors  

PTS.logit.LDV <- lrm(PTS_ai_reversed ~ Lag_lognonnegUS_fdi_totalpGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_PTS_ai_reversed,data=data.PTS.LDV,x=TRUE, y=TRUE)
PTS.logit.LDV.corr <- robcov(PTS.logit.LDV,data.PTS.LDV$country) #robust errors  

#Fariss xtpcse with LDV
data.Fariss.LDV <- as.data.frame(data.Fariss.LDV)
form   <- Fariss_HR ~ Lag_lognonnegUS_fdi_totalpGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_Fariss_HR
res.po <- plm(form,model="pooling",index=c("country","year"),data=data.Fariss.LDV)
summary(res.po)      
## PCSE a la Beck-Katz
res.BKtime  <- vcovBK(x=res.po,cluster="time")
ct.time     <- coeftest(res.po,vcov=res.BKtime)
print(ct.time)

# rename lagged values to get them into one row with stargazer; replace plm se's with corrected ones
names(CIRI.logit.LDV.corr$coefficients)
names(CIRI.logit.LDV.corr$coefficients)[names(CIRI.logit.LDV.corr$coefficients) == "Lag_CIRI_PHYSINT"] <- "Past"
rownames(CIRI.logit.LDV.corr$var)[rownames(CIRI.logit.LDV.corr$var)== "Lag_CIRI_PHYSINT"] <- "Past"
colnames(CIRI.logit.LDV.corr$var)[colnames(CIRI.logit.LDV.corr$var)== "Lag_CIRI_PHYSINT"] <- "Past"

names(PTS.logit.LDV.corr$coefficients)
names(PTS.logit.LDV.corr$coefficients)[names(PTS.logit.LDV.corr$coefficients) == "Lag_PTS_ai_reversed"] <- "Past"
rownames(PTS.logit.LDV.corr$var)[rownames(PTS.logit.LDV.corr$var)== "Lag_PTS_ai_reversed"] <- "Past"
colnames(PTS.logit.LDV.corr$var)[colnames(PTS.logit.LDV.corr$var)== "Lag_PTS_ai_reversed"] <- "Past"

res.po$vcov <- res.BKtime  # replacing the first uncorrected residual covariance structure with the corrected one see str()
names(res.po$coefficients)[names(res.po$coefficients) == "Lag_Fariss_HR"] <- "Past"
rownames(res.po$vcov)[rownames(res.po$vcov)== "Lag_Fariss_HR"] <- "Past"
colnames(res.po$vcov)[colnames(res.po$vcov)== "Lag_Fariss_HR"] <- "Past"

# Results Total UN FDI 
CIRI.logit.LDV.UN <- lrm(CIRI_PHYSINT ~ Lag_log_UN_FDI_stock_pgdp + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_CIRI_PHYSINT,data=data.CIRI.PHY1.LDV ,x=TRUE, y=TRUE)
CIRI.logit.LDV.UN.corr <- robcov(CIRI.logit.LDV.UN,data.CIRI.PHY1.LDV$country) #robust errors  

PTS.logit.LDV.UN <- lrm(PTS_ai_reversed ~ Lag_log_UN_FDI_stock_pgdp + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_PTS_ai_reversed,data=data.PTS1.LDV,x=TRUE, y=TRUE)
PTS.logit.LDV.UN.corr <- robcov(PTS.logit.LDV.UN,data.PTS1.LDV$country) #robust errors  

data.Fariss.1.LDV <- as.data.frame(data.Fariss1.LDV)
form   <- Fariss_HR ~ Lag_log_UN_FDI_stock_pgdp + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_Fariss_HR
res.po1 <- plm(form,model="pooling",index=c("country","year"),data=data.Fariss1.LDV)
summary(res.po1)      
## PCSE a la Beck-Katz
res.BKtime1  <- vcovBK(x=res.po1,cluster="time")
ct.time1     <- coeftest(res.po1,vcov=res.BKtime1)
print(ct.time1) # 

# rename lagged values to get them into one row
names(CIRI.logit.LDV.UN.corr$coefficients)
names(CIRI.logit.LDV.UN.corr$coefficients)[names(CIRI.logit.LDV.UN.corr$coefficients) == "Lag_CIRI_PHYSINT"] <- "Past"
rownames(CIRI.logit.LDV.UN.corr$var)[rownames(CIRI.logit.LDV.UN.corr$var)== "Lag_CIRI_PHYSINT"] <- "Past"
colnames(CIRI.logit.LDV.UN.corr$var)[colnames(CIRI.logit.LDV.UN.corr$var)== "Lag_CIRI_PHYSINT"] <- "Past"
names(PTS.logit.LDV.UN.corr$coefficients)
names(PTS.logit.LDV.UN.corr$coefficients)[names(PTS.logit.LDV.UN.corr$coefficients) == "Lag_PTS_ai_reversed"] <- "Past"
rownames(PTS.logit.LDV.UN.corr$var)[rownames(PTS.logit.LDV.UN.corr$var)== "Lag_PTS_ai_reversed"] <- "Past"
colnames(PTS.logit.LDV.UN.corr$var)[colnames(PTS.logit.LDV.UN.corr$var)== "Lag_PTS_ai_reversed"] <- "Past"

res.po1$vcov <- res.BKtime1  # replacing the first uncorrected residual covariance structure with the corrected one see str()
names(res.po1$coefficients)[names(res.po1$coefficients) == "Lag_Fariss_HR"] <- "Past"
rownames(res.po1$vcov)[rownames(res.po1$vcov)== "Lag_Fariss_HR"] <- "Past"
colnames(res.po1$vcov)[colnames(res.po1$vcov)== "Lag_Fariss_HR"] <- "Past"

# sectoral (baseline)####
CIRI.logit.LDV.excl <- lrm(CIRI_PHYSINT ~ Lag_lognonnegUS_fdi_foodpGDP + Lag_lognonnegUS_fdi_chemicalpGDP + Lag_lognonnegUS_fdi_prim_fab_metalpGDP + Lag_lognonnegUS_fdi_machinerypGDP + Lag_lognonnegUS_fdi_electricalpGDP + Lag_lognonnegUS_fdi_transportpGDP + Lag_lognonnegUS_fdi_whole_tradepGDP + Lag_lognonnegUS_fdi_depositorypGDP+ Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_CIRI_PHYSINT,data=data.CIRI.PHY.excl.LDV  ,x=TRUE, y=TRUE)
CIRI.logit.LDV.excl.corr <- robcov(CIRI.logit.LDV.excl,data.CIRI.PHY.excl.LDV$country) #robust errors  

PTS.logit.LDV.excl <- lrm(PTS_ai_reversed ~ Lag_lognonnegUS_fdi_foodpGDP + Lag_lognonnegUS_fdi_chemicalpGDP + Lag_lognonnegUS_fdi_prim_fab_metalpGDP + Lag_lognonnegUS_fdi_machinerypGDP + Lag_lognonnegUS_fdi_electricalpGDP + Lag_lognonnegUS_fdi_transportpGDP + Lag_lognonnegUS_fdi_whole_tradepGDP + Lag_lognonnegUS_fdi_depositorypGDP+ Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_PTS_ai_reversed,data=data.PTS.excl.LDV,x=TRUE, y=TRUE)
PTS.logit.LDV.excl.corr <- robcov(PTS.logit.LDV.excl,data.PTS.excl.LDV$country) 

data.Fariss.excl.LDV <- as.data.frame(data.Fariss.excl.LDV)
form   <- Fariss_HR ~  Lag_lognonnegUS_fdi_foodpGDP + Lag_lognonnegUS_fdi_chemicalpGDP + Lag_lognonnegUS_fdi_prim_fab_metalpGDP + Lag_lognonnegUS_fdi_machinerypGDP + Lag_lognonnegUS_fdi_electricalpGDP + Lag_lognonnegUS_fdi_transportpGDP + Lag_lognonnegUS_fdi_whole_tradepGDP + Lag_lognonnegUS_fdi_depositorypGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_Fariss_HR
res.po.excl <- plm(form,model="pooling",index=c("country","year"),data=data.Fariss.excl.LDV)
summary(res.po.excl)      
## PCSE a la Beck-Katz
res.BKtime.excl  <- vcovBK(x=res.po.excl,cluster="time")
ct.time.excl     <- coeftest(res.po.excl,vcov=res.BKtime.excl)
print(ct.time.excl)

# rename lagged values to get them into one row
names(CIRI.logit.LDV.excl.corr$coefficients)
names(CIRI.logit.LDV.excl.corr$coefficients)[names(CIRI.logit.LDV.excl.corr$coefficients) == "Lag_CIRI_PHYSINT"] <- "Past"
rownames(CIRI.logit.LDV.excl.corr$var)[rownames(CIRI.logit.LDV.excl.corr$var)== "Lag_CIRI_PHYSINT"] <- "Past"
colnames(CIRI.logit.LDV.excl.corr$var)[colnames(CIRI.logit.LDV.excl.corr$var)== "Lag_CIRI_PHYSINT"] <- "Past"
names(PTS.logit.LDV.excl.corr$coefficients)
names(PTS.logit.LDV.excl.corr$coefficients)[names(PTS.logit.LDV.excl.corr$coefficients) == "Lag_PTS_ai_reversed"] <- "Past"
rownames(PTS.logit.LDV.excl.corr$var)[rownames(PTS.logit.LDV.excl.corr$var)== "Lag_PTS_ai_reversed"] <- "Past"
colnames(PTS.logit.LDV.excl.corr$var)[colnames(PTS.logit.LDV.excl.corr$var)== "Lag_PTS_ai_reversed"] <- "Past"


res.po.excl$vcov <- res.BKtime.excl  # replacing the first uncorrected residual covariance structure with the corrected one see str()
names(res.po.excl$coefficients)[names(res.po.excl$coefficients) == "Lag_Fariss_HR"] <- "Past"
rownames(res.po.excl$vcov)[rownames(res.po.excl$vcov)== "Lag_Fariss_HR"] <- "Past"
colnames(res.po.excl$vcov)[colnames(res.po.excl$vcov)== "Lag_Fariss_HR"] <- "Past"

# ending 3 year lag with saving res objects####
CIRI.logit.LDV.UN.corr.3 <- CIRI.logit.LDV.UN.corr
PTS.logit.LDV.UN.corr.3 <- PTS.logit.LDV.UN.corr
res.po1.3 <- res.po1
CIRI.logit.LDV.corr.3 <- CIRI.logit.LDV.corr
PTS.logit.LDV.corr.3 <- PTS.logit.LDV.corr
res.po.3 <- res.po
CIRI.logit.LDV.excl.corr.3 <- CIRI.logit.LDV.excl.corr
PTS.logit.LDV.excl.corr.3 <- PTS.logit.LDV.excl.corr
res.po.excl.3 <- res.po.excl













#Main Models for 4 year lag####
t.nono <- t.nono.4

# data####
#US
data.CIRI.PHY.LDV <- na.omit(t.nono[,cbind("CIRI_PHYSINT","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_totalpGDP","Lag_CIRI_PHYSINT", "country","year")])
data.PTS.LDV <- na.omit(t.nono[,cbind("PTS_ai_reversed","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_totalpGDP", "Lag_PTS_ai_reversed", "country","year")])
data.Fariss.LDV <- na.omit(t.nono[,cbind("Fariss_HR","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_totalpGDP", "Lag_Fariss_HR","country","year")]) 
data.Fariss.LDV$country <- factor(as.character(data.Fariss.LDV$country)) 
# UN
data.CIRI.PHY1.LDV <- na.omit(t.nono[,cbind("CIRI_PHYSINT","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_log_UN_FDI_stock_pgdp" ,"Lag_CIRI_PHYSINT","country","year")])
data.PTS1.LDV <- na.omit(t.nono[,cbind("PTS_ai_reversed","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_log_UN_FDI_stock_pgdp","Lag_PTS_ai_reversed", "country","year")])
data.Fariss1.LDV <- na.omit(t.nono[,cbind("Fariss_HR","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_log_UN_FDI_stock_pgdp", "Lag_Fariss_HR","country","year")])
# data for SECTORAL 
data.CIRI.PHY.excl.LDV <- na.omit(t.nono[,cbind("CIRI_PHYSINT","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_foodpGDP","Lag_lognonnegUS_fdi_chemicalpGDP","Lag_lognonnegUS_fdi_prim_fab_metalpGDP","Lag_lognonnegUS_fdi_machinerypGDP","Lag_lognonnegUS_fdi_electricalpGDP","Lag_lognonnegUS_fdi_transportpGDP","Lag_lognonnegUS_fdi_whole_tradepGDP","Lag_lognonnegUS_fdi_depositorypGDP","Lag_CIRI_PHYSINT", "country","year")])
data.PTS.excl.LDV  <- na.omit(t.nono[,cbind("PTS_ai_reversed","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_foodpGDP","Lag_lognonnegUS_fdi_chemicalpGDP","Lag_lognonnegUS_fdi_prim_fab_metalpGDP","Lag_lognonnegUS_fdi_machinerypGDP","Lag_lognonnegUS_fdi_electricalpGDP","Lag_lognonnegUS_fdi_transportpGDP","Lag_lognonnegUS_fdi_whole_tradepGDP","Lag_lognonnegUS_fdi_depositorypGDP","Lag_PTS_ai_reversed","country","year")])
data.Fariss.excl.LDV <- na.omit(t.nono[,cbind("Fariss_HR","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_foodpGDP","Lag_lognonnegUS_fdi_chemicalpGDP","Lag_lognonnegUS_fdi_prim_fab_metalpGDP","Lag_lognonnegUS_fdi_machinerypGDP","Lag_lognonnegUS_fdi_electricalpGDP","Lag_lognonnegUS_fdi_transportpGDP","Lag_lognonnegUS_fdi_whole_tradepGDP","Lag_lognonnegUS_fdi_depositorypGDP","Lag_Fariss_HR","country","year")])

# models 4 year lag####
CIRI.logit.LDV <- lrm(CIRI_PHYSINT ~ Lag_lognonnegUS_fdi_totalpGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_CIRI_PHYSINT,data=data.CIRI.PHY.LDV,x=TRUE, y=TRUE)
CIRI.logit.LDV.corr <- robcov(CIRI.logit.LDV,data.CIRI.PHY.LDV$country) #robust errors  

PTS.logit.LDV <- lrm(PTS_ai_reversed ~ Lag_lognonnegUS_fdi_totalpGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_PTS_ai_reversed,data=data.PTS.LDV,x=TRUE, y=TRUE)
PTS.logit.LDV.corr <- robcov(PTS.logit.LDV,data.PTS.LDV$country) #robust errors  

#Fariss xtpcse with LDV
data.Fariss.LDV <- as.data.frame(data.Fariss.LDV)
form   <- Fariss_HR ~ Lag_lognonnegUS_fdi_totalpGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_Fariss_HR
res.po <- plm(form,model="pooling",index=c("country","year"),data=data.Fariss.LDV)
summary(res.po)      
## PCSE a la Beck-Katz
res.BKtime  <- vcovBK(x=res.po,cluster="time")
ct.time     <- coeftest(res.po,vcov=res.BKtime)
print(ct.time)

# rename lagged values to get them into one row with stargazer; replace plm se's with corrected ones
names(CIRI.logit.LDV.corr$coefficients)
names(CIRI.logit.LDV.corr$coefficients)[names(CIRI.logit.LDV.corr$coefficients) == "Lag_CIRI_PHYSINT"] <- "Past"
rownames(CIRI.logit.LDV.corr$var)[rownames(CIRI.logit.LDV.corr$var)== "Lag_CIRI_PHYSINT"] <- "Past"
colnames(CIRI.logit.LDV.corr$var)[colnames(CIRI.logit.LDV.corr$var)== "Lag_CIRI_PHYSINT"] <- "Past"

names(PTS.logit.LDV.corr$coefficients)
names(PTS.logit.LDV.corr$coefficients)[names(PTS.logit.LDV.corr$coefficients) == "Lag_PTS_ai_reversed"] <- "Past"
rownames(PTS.logit.LDV.corr$var)[rownames(PTS.logit.LDV.corr$var)== "Lag_PTS_ai_reversed"] <- "Past"
colnames(PTS.logit.LDV.corr$var)[colnames(PTS.logit.LDV.corr$var)== "Lag_PTS_ai_reversed"] <- "Past"

res.po$vcov <- res.BKtime  # replacing the first uncorrected residual covariance structure with the corrected one see str()
names(res.po$coefficients)[names(res.po$coefficients) == "Lag_Fariss_HR"] <- "Past"
rownames(res.po$vcov)[rownames(res.po$vcov)== "Lag_Fariss_HR"] <- "Past"
colnames(res.po$vcov)[colnames(res.po$vcov)== "Lag_Fariss_HR"] <- "Past"

# Results Total UN FDI 
CIRI.logit.LDV.UN <- lrm(CIRI_PHYSINT ~ Lag_log_UN_FDI_stock_pgdp + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_CIRI_PHYSINT,data=data.CIRI.PHY1.LDV ,x=TRUE, y=TRUE)
CIRI.logit.LDV.UN.corr <- robcov(CIRI.logit.LDV.UN,data.CIRI.PHY1.LDV$country) #robust errors  

PTS.logit.LDV.UN <- lrm(PTS_ai_reversed ~ Lag_log_UN_FDI_stock_pgdp + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_PTS_ai_reversed,data=data.PTS1.LDV,x=TRUE, y=TRUE)
PTS.logit.LDV.UN.corr <- robcov(PTS.logit.LDV.UN,data.PTS1.LDV$country) #robust errors  

data.Fariss.1.LDV <- as.data.frame(data.Fariss1.LDV)
form   <- Fariss_HR ~ Lag_log_UN_FDI_stock_pgdp + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_Fariss_HR
res.po1 <- plm(form,model="pooling",index=c("country","year"),data=data.Fariss1.LDV)
summary(res.po1)      
## PCSE a la Beck-Katz
res.BKtime1  <- vcovBK(x=res.po1,cluster="time")
ct.time1     <- coeftest(res.po1,vcov=res.BKtime1)
print(ct.time1) # 

# rename lagged values to get them into one row
names(CIRI.logit.LDV.UN.corr$coefficients)
names(CIRI.logit.LDV.UN.corr$coefficients)[names(CIRI.logit.LDV.UN.corr$coefficients) == "Lag_CIRI_PHYSINT"] <- "Past"
rownames(CIRI.logit.LDV.UN.corr$var)[rownames(CIRI.logit.LDV.UN.corr$var)== "Lag_CIRI_PHYSINT"] <- "Past"
colnames(CIRI.logit.LDV.UN.corr$var)[colnames(CIRI.logit.LDV.UN.corr$var)== "Lag_CIRI_PHYSINT"] <- "Past"
names(PTS.logit.LDV.UN.corr$coefficients)
names(PTS.logit.LDV.UN.corr$coefficients)[names(PTS.logit.LDV.UN.corr$coefficients) == "Lag_PTS_ai_reversed"] <- "Past"
rownames(PTS.logit.LDV.UN.corr$var)[rownames(PTS.logit.LDV.UN.corr$var)== "Lag_PTS_ai_reversed"] <- "Past"
colnames(PTS.logit.LDV.UN.corr$var)[colnames(PTS.logit.LDV.UN.corr$var)== "Lag_PTS_ai_reversed"] <- "Past"

res.po1$vcov <- res.BKtime1  # replacing the first uncorrected residual covariance structure with the corrected one see str()
names(res.po1$coefficients)[names(res.po1$coefficients) == "Lag_Fariss_HR"] <- "Past"
rownames(res.po1$vcov)[rownames(res.po1$vcov)== "Lag_Fariss_HR"] <- "Past"
colnames(res.po1$vcov)[colnames(res.po1$vcov)== "Lag_Fariss_HR"] <- "Past"



# sectoral (baseline)####
CIRI.logit.LDV.excl <- lrm(CIRI_PHYSINT ~ Lag_lognonnegUS_fdi_foodpGDP + Lag_lognonnegUS_fdi_chemicalpGDP + Lag_lognonnegUS_fdi_prim_fab_metalpGDP + Lag_lognonnegUS_fdi_machinerypGDP + Lag_lognonnegUS_fdi_electricalpGDP + Lag_lognonnegUS_fdi_transportpGDP + Lag_lognonnegUS_fdi_whole_tradepGDP + Lag_lognonnegUS_fdi_depositorypGDP+ Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_CIRI_PHYSINT,data=data.CIRI.PHY.excl.LDV  ,x=TRUE, y=TRUE)
CIRI.logit.LDV.excl.corr <- robcov(CIRI.logit.LDV.excl,data.CIRI.PHY.excl.LDV$country) #robust errors  

PTS.logit.LDV.excl <- lrm(PTS_ai_reversed ~ Lag_lognonnegUS_fdi_foodpGDP + Lag_lognonnegUS_fdi_chemicalpGDP + Lag_lognonnegUS_fdi_prim_fab_metalpGDP + Lag_lognonnegUS_fdi_machinerypGDP + Lag_lognonnegUS_fdi_electricalpGDP + Lag_lognonnegUS_fdi_transportpGDP + Lag_lognonnegUS_fdi_whole_tradepGDP + Lag_lognonnegUS_fdi_depositorypGDP+ Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_PTS_ai_reversed,data=data.PTS.excl.LDV,x=TRUE, y=TRUE)
PTS.logit.LDV.excl.corr <- robcov(PTS.logit.LDV.excl,data.PTS.excl.LDV$country) 

data.Fariss.excl.LDV <- as.data.frame(data.Fariss.excl.LDV)
form   <- Fariss_HR ~  Lag_lognonnegUS_fdi_foodpGDP + Lag_lognonnegUS_fdi_chemicalpGDP + Lag_lognonnegUS_fdi_prim_fab_metalpGDP + Lag_lognonnegUS_fdi_machinerypGDP + Lag_lognonnegUS_fdi_electricalpGDP + Lag_lognonnegUS_fdi_transportpGDP + Lag_lognonnegUS_fdi_whole_tradepGDP + Lag_lognonnegUS_fdi_depositorypGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_Fariss_HR
res.po.excl <- plm(form,model="pooling",index=c("country","year"),data=data.Fariss.excl.LDV)
summary(res.po.excl)      
## PCSE a la Beck-Katz
res.BKtime.excl  <- vcovBK(x=res.po.excl,cluster="time")
ct.time.excl     <- coeftest(res.po.excl,vcov=res.BKtime.excl)
print(ct.time.excl)

# rename lagged values to get them into one row
names(CIRI.logit.LDV.excl.corr$coefficients)
names(CIRI.logit.LDV.excl.corr$coefficients)[names(CIRI.logit.LDV.excl.corr$coefficients) == "Lag_CIRI_PHYSINT"] <- "Past"
rownames(CIRI.logit.LDV.excl.corr$var)[rownames(CIRI.logit.LDV.excl.corr$var)== "Lag_CIRI_PHYSINT"] <- "Past"
colnames(CIRI.logit.LDV.excl.corr$var)[colnames(CIRI.logit.LDV.excl.corr$var)== "Lag_CIRI_PHYSINT"] <- "Past"
names(PTS.logit.LDV.excl.corr$coefficients)
names(PTS.logit.LDV.excl.corr$coefficients)[names(PTS.logit.LDV.excl.corr$coefficients) == "Lag_PTS_ai_reversed"] <- "Past"
rownames(PTS.logit.LDV.excl.corr$var)[rownames(PTS.logit.LDV.excl.corr$var)== "Lag_PTS_ai_reversed"] <- "Past"
colnames(PTS.logit.LDV.excl.corr$var)[colnames(PTS.logit.LDV.excl.corr$var)== "Lag_PTS_ai_reversed"] <- "Past"


res.po.excl$vcov <- res.BKtime.excl  # replacing the first uncorrected residual covariance structure with the corrected one see str()
names(res.po.excl$coefficients)[names(res.po.excl$coefficients) == "Lag_Fariss_HR"] <- "Past"
rownames(res.po.excl$vcov)[rownames(res.po.excl$vcov)== "Lag_Fariss_HR"] <- "Past"
colnames(res.po.excl$vcov)[colnames(res.po.excl$vcov)== "Lag_Fariss_HR"] <- "Past"


# ending 4 year lag with saving res objects####
CIRI.logit.LDV.UN.corr.4 <- CIRI.logit.LDV.UN.corr
PTS.logit.LDV.UN.corr.4 <- PTS.logit.LDV.UN.corr
res.po1.4 <- res.po1
CIRI.logit.LDV.corr.4 <- CIRI.logit.LDV.corr
PTS.logit.LDV.corr.4 <- PTS.logit.LDV.corr
res.po.4 <- res.po
CIRI.logit.LDV.excl.corr.4 <- CIRI.logit.LDV.excl.corr
PTS.logit.LDV.excl.corr.4 <- PTS.logit.LDV.excl.corr
res.po.excl.4 <- res.po.excl













#Main Models for 5 year lag####
t.nono <- t.nono.5

# data####
#US
data.CIRI.PHY.LDV <- na.omit(t.nono[,cbind("CIRI_PHYSINT","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_totalpGDP","Lag_CIRI_PHYSINT", "country","year")])
data.PTS.LDV <- na.omit(t.nono[,cbind("PTS_ai_reversed","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_totalpGDP", "Lag_PTS_ai_reversed", "country","year")])
data.Fariss.LDV <- na.omit(t.nono[,cbind("Fariss_HR","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_totalpGDP", "Lag_Fariss_HR","country","year")]) 
data.Fariss.LDV$country <- factor(as.character(data.Fariss.LDV$country)) 
# UN
data.CIRI.PHY1.LDV <- na.omit(t.nono[,cbind("CIRI_PHYSINT","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_log_UN_FDI_stock_pgdp" ,"Lag_CIRI_PHYSINT","country","year")])
data.PTS1.LDV <- na.omit(t.nono[,cbind("PTS_ai_reversed","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_log_UN_FDI_stock_pgdp","Lag_PTS_ai_reversed", "country","year")])
data.Fariss1.LDV <- na.omit(t.nono[,cbind("Fariss_HR","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_log_UN_FDI_stock_pgdp", "Lag_Fariss_HR","country","year")])
# data for SECTORAL 
data.CIRI.PHY.excl.LDV <- na.omit(t.nono[,cbind("CIRI_PHYSINT","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_foodpGDP","Lag_lognonnegUS_fdi_chemicalpGDP","Lag_lognonnegUS_fdi_prim_fab_metalpGDP","Lag_lognonnegUS_fdi_machinerypGDP","Lag_lognonnegUS_fdi_electricalpGDP","Lag_lognonnegUS_fdi_transportpGDP","Lag_lognonnegUS_fdi_whole_tradepGDP","Lag_lognonnegUS_fdi_depositorypGDP","Lag_CIRI_PHYSINT", "country","year")])
data.PTS.excl.LDV  <- na.omit(t.nono[,cbind("PTS_ai_reversed","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_foodpGDP","Lag_lognonnegUS_fdi_chemicalpGDP","Lag_lognonnegUS_fdi_prim_fab_metalpGDP","Lag_lognonnegUS_fdi_machinerypGDP","Lag_lognonnegUS_fdi_electricalpGDP","Lag_lognonnegUS_fdi_transportpGDP","Lag_lognonnegUS_fdi_whole_tradepGDP","Lag_lognonnegUS_fdi_depositorypGDP","Lag_PTS_ai_reversed","country","year")])
data.Fariss.excl.LDV <- na.omit(t.nono[,cbind("Fariss_HR","Lag_logtrade","Lag_logGDPpc","Lag_logpopulation","Lag_polity2","Lag_confl","Lag_lognonnegUS_fdi_foodpGDP","Lag_lognonnegUS_fdi_chemicalpGDP","Lag_lognonnegUS_fdi_prim_fab_metalpGDP","Lag_lognonnegUS_fdi_machinerypGDP","Lag_lognonnegUS_fdi_electricalpGDP","Lag_lognonnegUS_fdi_transportpGDP","Lag_lognonnegUS_fdi_whole_tradepGDP","Lag_lognonnegUS_fdi_depositorypGDP","Lag_Fariss_HR","country","year")])

# models 5 year lag####
CIRI.logit.LDV <- lrm(CIRI_PHYSINT ~ Lag_lognonnegUS_fdi_totalpGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_CIRI_PHYSINT,data=data.CIRI.PHY.LDV,x=TRUE, y=TRUE)
CIRI.logit.LDV.corr <- robcov(CIRI.logit.LDV,data.CIRI.PHY.LDV$country) #robust errors  

PTS.logit.LDV <- lrm(PTS_ai_reversed ~ Lag_lognonnegUS_fdi_totalpGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_PTS_ai_reversed,data=data.PTS.LDV,x=TRUE, y=TRUE)
PTS.logit.LDV.corr <- robcov(PTS.logit.LDV,data.PTS.LDV$country) #robust errors  

#Fariss xtpcse with LDV
data.Fariss.LDV <- as.data.frame(data.Fariss.LDV)
form   <- Fariss_HR ~ Lag_lognonnegUS_fdi_totalpGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_Fariss_HR
res.po <- plm(form,model="pooling",index=c("country","year"),data=data.Fariss.LDV)
summary(res.po)      
## PCSE a la Beck-Katz
res.BKtime  <- vcovBK(x=res.po,cluster="time")
ct.time     <- coeftest(res.po,vcov=res.BKtime)
print(ct.time)

# rename lagged values to get them into one row with stargazer; replace plm se's with corrected ones
names(CIRI.logit.LDV.corr$coefficients)
names(CIRI.logit.LDV.corr$coefficients)[names(CIRI.logit.LDV.corr$coefficients) == "Lag_CIRI_PHYSINT"] <- "Past"
rownames(CIRI.logit.LDV.corr$var)[rownames(CIRI.logit.LDV.corr$var)== "Lag_CIRI_PHYSINT"] <- "Past"
colnames(CIRI.logit.LDV.corr$var)[colnames(CIRI.logit.LDV.corr$var)== "Lag_CIRI_PHYSINT"] <- "Past"

names(PTS.logit.LDV.corr$coefficients)
names(PTS.logit.LDV.corr$coefficients)[names(PTS.logit.LDV.corr$coefficients) == "Lag_PTS_ai_reversed"] <- "Past"
rownames(PTS.logit.LDV.corr$var)[rownames(PTS.logit.LDV.corr$var)== "Lag_PTS_ai_reversed"] <- "Past"
colnames(PTS.logit.LDV.corr$var)[colnames(PTS.logit.LDV.corr$var)== "Lag_PTS_ai_reversed"] <- "Past"

res.po$vcov <- res.BKtime  # replacing the first uncorrected residual covariance structure with the corrected one see str()
names(res.po$coefficients)[names(res.po$coefficients) == "Lag_Fariss_HR"] <- "Past"
rownames(res.po$vcov)[rownames(res.po$vcov)== "Lag_Fariss_HR"] <- "Past"
colnames(res.po$vcov)[colnames(res.po$vcov)== "Lag_Fariss_HR"] <- "Past"

# Results Total UN FDI 
CIRI.logit.LDV.UN <- lrm(CIRI_PHYSINT ~ Lag_log_UN_FDI_stock_pgdp + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_CIRI_PHYSINT,data=data.CIRI.PHY1.LDV ,x=TRUE, y=TRUE)
CIRI.logit.LDV.UN.corr <- robcov(CIRI.logit.LDV.UN,data.CIRI.PHY1.LDV$country) #robust errors  

PTS.logit.LDV.UN <- lrm(PTS_ai_reversed ~ Lag_log_UN_FDI_stock_pgdp + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_PTS_ai_reversed,data=data.PTS1.LDV,x=TRUE, y=TRUE)
PTS.logit.LDV.UN.corr <- robcov(PTS.logit.LDV.UN,data.PTS1.LDV$country) #robust errors  

data.Fariss.1.LDV <- as.data.frame(data.Fariss1.LDV)
form   <- Fariss_HR ~ Lag_log_UN_FDI_stock_pgdp + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_Fariss_HR
res.po1 <- plm(form,model="pooling",index=c("country","year"),data=data.Fariss1.LDV)
summary(res.po1)      
## PCSE a la Beck-Katz
res.BKtime1  <- vcovBK(x=res.po1,cluster="time")
ct.time1     <- coeftest(res.po1,vcov=res.BKtime1)
print(ct.time1) # 

# rename lagged values to get them into one row
names(CIRI.logit.LDV.UN.corr$coefficients)
names(CIRI.logit.LDV.UN.corr$coefficients)[names(CIRI.logit.LDV.UN.corr$coefficients) == "Lag_CIRI_PHYSINT"] <- "Past"
rownames(CIRI.logit.LDV.UN.corr$var)[rownames(CIRI.logit.LDV.UN.corr$var)== "Lag_CIRI_PHYSINT"] <- "Past"
colnames(CIRI.logit.LDV.UN.corr$var)[colnames(CIRI.logit.LDV.UN.corr$var)== "Lag_CIRI_PHYSINT"] <- "Past"
names(PTS.logit.LDV.UN.corr$coefficients)
names(PTS.logit.LDV.UN.corr$coefficients)[names(PTS.logit.LDV.UN.corr$coefficients) == "Lag_PTS_ai_reversed"] <- "Past"
rownames(PTS.logit.LDV.UN.corr$var)[rownames(PTS.logit.LDV.UN.corr$var)== "Lag_PTS_ai_reversed"] <- "Past"
colnames(PTS.logit.LDV.UN.corr$var)[colnames(PTS.logit.LDV.UN.corr$var)== "Lag_PTS_ai_reversed"] <- "Past"

res.po1$vcov <- res.BKtime1  # replacing the first uncorrected residual covariance structure with the corrected one see str()
names(res.po1$coefficients)[names(res.po1$coefficients) == "Lag_Fariss_HR"] <- "Past"
rownames(res.po1$vcov)[rownames(res.po1$vcov)== "Lag_Fariss_HR"] <- "Past"
colnames(res.po1$vcov)[colnames(res.po1$vcov)== "Lag_Fariss_HR"] <- "Past"


# sectoral (baseline)####
CIRI.logit.LDV.excl <- lrm(CIRI_PHYSINT ~ Lag_lognonnegUS_fdi_foodpGDP + Lag_lognonnegUS_fdi_chemicalpGDP + Lag_lognonnegUS_fdi_prim_fab_metalpGDP + Lag_lognonnegUS_fdi_machinerypGDP + Lag_lognonnegUS_fdi_electricalpGDP + Lag_lognonnegUS_fdi_transportpGDP + Lag_lognonnegUS_fdi_whole_tradepGDP + Lag_lognonnegUS_fdi_depositorypGDP+ Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_CIRI_PHYSINT,data=data.CIRI.PHY.excl.LDV  ,x=TRUE, y=TRUE)
CIRI.logit.LDV.excl.corr <- robcov(CIRI.logit.LDV.excl,data.CIRI.PHY.excl.LDV$country) #robust errors  

PTS.logit.LDV.excl <- lrm(PTS_ai_reversed ~ Lag_lognonnegUS_fdi_foodpGDP + Lag_lognonnegUS_fdi_chemicalpGDP + Lag_lognonnegUS_fdi_prim_fab_metalpGDP + Lag_lognonnegUS_fdi_machinerypGDP + Lag_lognonnegUS_fdi_electricalpGDP + Lag_lognonnegUS_fdi_transportpGDP + Lag_lognonnegUS_fdi_whole_tradepGDP + Lag_lognonnegUS_fdi_depositorypGDP+ Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_PTS_ai_reversed,data=data.PTS.excl.LDV,x=TRUE, y=TRUE)
PTS.logit.LDV.excl.corr <- robcov(PTS.logit.LDV.excl,data.PTS.excl.LDV$country) 

data.Fariss.excl.LDV <- as.data.frame(data.Fariss.excl.LDV)
form   <- Fariss_HR ~  Lag_lognonnegUS_fdi_foodpGDP + Lag_lognonnegUS_fdi_chemicalpGDP + Lag_lognonnegUS_fdi_prim_fab_metalpGDP + Lag_lognonnegUS_fdi_machinerypGDP + Lag_lognonnegUS_fdi_electricalpGDP + Lag_lognonnegUS_fdi_transportpGDP + Lag_lognonnegUS_fdi_whole_tradepGDP + Lag_lognonnegUS_fdi_depositorypGDP + Lag_logtrade + Lag_logGDPpc + Lag_logpopulation + Lag_polity2 + Lag_confl + Lag_Fariss_HR
res.po.excl <- plm(form,model="pooling",index=c("country","year"),data=data.Fariss.excl.LDV)
summary(res.po.excl)      
## PCSE a la Beck-Katz
res.BKtime.excl  <- vcovBK(x=res.po.excl,cluster="time")
ct.time.excl     <- coeftest(res.po.excl,vcov=res.BKtime.excl)
print(ct.time.excl)

# rename lagged values to get them into one row
names(CIRI.logit.LDV.excl.corr$coefficients)
names(CIRI.logit.LDV.excl.corr$coefficients)[names(CIRI.logit.LDV.excl.corr$coefficients) == "Lag_CIRI_PHYSINT"] <- "Past"
rownames(CIRI.logit.LDV.excl.corr$var)[rownames(CIRI.logit.LDV.excl.corr$var)== "Lag_CIRI_PHYSINT"] <- "Past"
colnames(CIRI.logit.LDV.excl.corr$var)[colnames(CIRI.logit.LDV.excl.corr$var)== "Lag_CIRI_PHYSINT"] <- "Past"
names(PTS.logit.LDV.excl.corr$coefficients)
names(PTS.logit.LDV.excl.corr$coefficients)[names(PTS.logit.LDV.excl.corr$coefficients) == "Lag_PTS_ai_reversed"] <- "Past"
rownames(PTS.logit.LDV.excl.corr$var)[rownames(PTS.logit.LDV.excl.corr$var)== "Lag_PTS_ai_reversed"] <- "Past"
colnames(PTS.logit.LDV.excl.corr$var)[colnames(PTS.logit.LDV.excl.corr$var)== "Lag_PTS_ai_reversed"] <- "Past"


res.po.excl$vcov <- res.BKtime.excl  # replacing the first uncorrected residual covariance structure with the corrected one see str()
names(res.po.excl$coefficients)[names(res.po.excl$coefficients) == "Lag_Fariss_HR"] <- "Past"
rownames(res.po.excl$vcov)[rownames(res.po.excl$vcov)== "Lag_Fariss_HR"] <- "Past"
colnames(res.po.excl$vcov)[colnames(res.po.excl$vcov)== "Lag_Fariss_HR"] <- "Past"

# ending 5 year lag with saving res objects####
CIRI.logit.LDV.UN.corr.5 <- CIRI.logit.LDV.UN.corr
PTS.logit.LDV.UN.corr.5 <- PTS.logit.LDV.UN.corr
res.po1.5 <- res.po1
CIRI.logit.LDV.corr.5 <- CIRI.logit.LDV.corr
PTS.logit.LDV.corr.5 <- PTS.logit.LDV.corr
res.po.5 <- res.po
CIRI.logit.LDV.excl.corr.5 <- CIRI.logit.LDV.excl.corr
PTS.logit.LDV.excl.corr.5 <- PTS.logit.LDV.excl.corr
res.po.excl.5 <- res.po.excl




# make one big lag table total U.S. FDI
varnames <- c("U.S. FDI")
dvnames <- c("CIRI","PTS","Latent","CIRI","PTS","Latent","CIRI","PTS","Latent","CIRI","PTS","Latent")
stargazer( CIRI.logit.LDV.corr.2,PTS.logit.LDV.corr.2,res.po.2,
           CIRI.logit.LDV.corr.3,PTS.logit.LDV.corr.3,res.po.3,
           CIRI.logit.LDV.corr.4,PTS.logit.LDV.corr.4,res.po.4,
           CIRI.logit.LDV.corr.5,PTS.logit.LDV.corr.5,res.po.5,
           covariate.labels=varnames, dep.var.labels=dvnames, 
           column.labels   = c("2-year lag", "3-year lag","4-year lag","5-year lag"),
           column.separate = c(3, 3,3,3),
           omit=c("y>=1","y>=2","y>=3","y>=4","y>=5","y>=6","y>=7","y>=8","factor","Constant","(Intercept)","Lag_logtrade", "Lag_logGDPpc", "Lag_logpopulation", "Lag_polity2", "Lag_confl", "Past"),omit.stat = c("f","ser","chi2"),
           digits       = 2,
           digits.extra = 1,
           nobs=T,title="Total U.S. FDI and Personal Integrity Rights, 2-5 year lags",no.space=T,font.size="scriptsize",model.names = T, notes=c("Ordered logit with country-clustered standard errors. OLS with panel corrected standard errors.","Ordered Logit Models: Nagelkerke R-squared. FDI is per GDP and logged. Control variables as in main models (not displayed)."),add.lines = list(c("Years", "1984-2010", "1984-2010","1984-2010","1985-2010","1985-2010","1985-2010","1986-2010","1986-2010","1986-2010","1987-2010","1987-2010","1987-2010")),notes.align="l",type="text",float.env="sidewaystable",label="totalUS.2to5yearlag.tex",out="totalUS.2to5yearlag.tex")


# make one big lag table sectoral FDI
varnames <- c("Food","Chemical","Metal","Machinery","Electrical","Transport", "Whole. Trade","Depository")
dvnames <- c("CIRI","PTS","Latent","CIRI","PTS","Latent","CIRI","PTS","Latent","CIRI","PTS","Latent")
stargazer(CIRI.logit.LDV.excl.corr.2,PTS.logit.LDV.excl.corr.2,res.po.excl.2,
          CIRI.logit.LDV.excl.corr.3,PTS.logit.LDV.excl.corr.3,res.po.excl.3,
          CIRI.logit.LDV.excl.corr.4,PTS.logit.LDV.excl.corr.4,res.po.excl.4,
          CIRI.logit.LDV.excl.corr.5,PTS.logit.LDV.excl.corr.5,res.po.excl.5,
          covariate.labels=varnames, dep.var.labels=dvnames, 
          column.labels   = c("2-year lag", "3-year lag","4-year lag","5-year lag"),
          column.separate = c(3, 3,3,3),
          omit=c("y>=1","y>=2","y>=3","y>=4","y>=5","y>=6","y>=7","y>=8","factor","Constant","(Intercept)","Lag_logtrade", "Lag_logGDPpc", "Lag_logpopulation", "Lag_polity2", "Lag_confl", "Past"), 
          omit.stat = c("f","ser","chi2"),
          digits       = 2,
          digits.extra = 1,
          nobs=T,title="Sectoral FDI and Personal Integrity Rights, 2-5 year lags",no.space=T,font.size="scriptsize",model.names = T, notes=c("Ordered logit with country-clustered standard errors. OLS with panel corrected standard errors.","Ordered Logit Models: Nagelkerke R-squared. FDI is per GDP and logged. Control variables as in main models (not displayed)."),add.lines = list(c("Years", "1984-2010", "1984-2010","1984-2010","1985-2010","1985-2010","1985-2010","1986-2010","1986-2010","1986-2010","1987-2010","1987-2010","1987-2010")),notes.align="l",type="text",float.env="sidewaystable",label="Exclusive.2to5yearlag.tex",out="Exclusive.2to5yearlag.tex")


